answersLogoWhite

0


Best Answer

The abstract keyword is used to denote abstract classes and abstract methods in Java.

An abstract method is a method which is defined, but not implemented:

public abstract void doStuff();

An abstract class is a class which contains one or more abstract methods*, and which cannot be instantiated directly. All subclasses of an abstract class must implement the abstract methods.

* Note that abstract classes can include no abstract methods, but this rather defeats the purpose of using an abstract class.

The quintessential example of an abstract class is the Shape class.

// Definition of our abstract class

public abstract class Shape {

// Notice how we can actually declare and implement variables and methods.

// This is what differentiates between an abstract class and an interface.

// The location of this shape

private double x,y;

// Change our location - all subclasses will have this by default

public void moveTo(final double newX, final double newY) {

x = newX;

y = newY;

}

// Definitions of our abstract classes.

// All classes which extend from Shape must implement these.

public abstract double getArea();

public abstract double getPerimiter();

}

// Definition of our concrete example class

public class Rectangle extends Shape {

// Beyond the x,y location of Shape, Rectangle must have width and height

private double width, height;

// Implement abstract methods

public double getArea() {

return width * height;

}

public double getPerimiter() {

return width + width + height + height;

}

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Abstract Methods

An abstract method is a method that's been declared as abstract but not implemented. In other words, the method contains no code. You mark a method abstract when you want to force subclasses to provide the implementation.

Ex: public abstract void showSample();

Notice that the abstract method ends with a semicolon instead of curly braces. It is illegal to have even a single abstract method in a class that is not explicitly declared abstract! Look at the following illegal class:

public class IllegalAbstractClass{

public abstract void test();

}

The preceding class will produce the following error if you try to compile it:

IllegalClass.java:1: class IllegalAbstractClass must be declared

abstract.

It does not define void test() from class IllegalAbstractClass.

public class IllegalAbstractClass {

1 error

You can, however, have an abstract class with no abstract methods. The following example will compile fine:

public abstract class LegalAbstractClass{

void test() {

// you can write lots of code here

}

}

In the preceding example, test() is not abstract. Three different clues tell you it's not an abstract method:

• The method is not marked abstract.

• The method declaration includes curly braces, as opposed to ending in a semicolon. In other words, the method has a method body.

• The method contains actual implementation code.

Any class that extends an abstract class must implement all abstract methods of the superclass, unless the subclass is also abstract. The rule is this:

The first concrete subclass of an abstract class must implement all abstract methods of the superclass.

Concrete just means nonabstract, so if you have an abstract class extending another abstract class, the abstract subclass doesn't need to provide implementations for the inherited abstract methods. Sooner or later, though, somebody's going to make a nonabstract subclass (in other words, a class that can be instantiated), and that subclass will have to implement all the abstract methods from up the inheritance tree.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain the meaning of abstract keyword in relation to classes and methods?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why don't use abstract keyword when we declare a method in interface?

The abstract keyword signifies that the particular method will have no features in the class where it is declared and it is upto the child class to provide the functionality. In case of an interface, the method is already abstract by default and has no code inside it. So there is no actual point in using the abstract keyword there.


What is the of abatract keyword in java 1.7?

"abstract" is used for a class that is not supposed to be instantiated directly. The reason this is used is because the class is not complete; the details are supposed to be filled out in subclasses (derived classes). An abstract class will usually have one or more abstract methods; a method that is declared by name (to make sure derived classes have this method), but without a method body.


Is it possible to declare a variable with abstract keyword?

yes


What is the need to create an abstract class without abstract?

Actually there is no need & most importantly - you cannot create an abstract class without using the abstract keyword


What are abstract classin java?

An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.Ex:abstract class Parent {public abstract String getSon();public abstract String getDaughter();........//More methods that contain specific behaviour/code in them}The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.i.e., the below piece of code will not work. The code will not even compile.Parent object = new Parent();Purpose of Abstract Classes:Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.A Child Class extending the Abstract Class:public class Child extends Parent {public String getSon() {return "Sons Name";}public String getDaughter(){return "Daughters Name";}...... //Code specific to the Child class}

Related questions

What happens if an abstract modifier is applied to class. Explain?

The classes which have one or more abstract methods are abstract. To declare a class as abstract, use the abstract keyword in front of the class keyword, before the class declaration. Abstract classes cannot be instantiated. Similarly the new keyword cannot be used to create an object of the abstract class. Remember that the constructors and static variables cannot be declared as abstract. Any subclass of an abstract class must either implement all of the abstract methods in the superclass or be itself declared abstract.


Is it possible to create a Abstract class without abstract keyword?

no


Can you declare a variable with abstract keyword?

no it is not possible


Why don't use abstract keyword when we declare a method in interface?

The abstract keyword signifies that the particular method will have no features in the class where it is declared and it is upto the child class to provide the functionality. In case of an interface, the method is already abstract by default and has no code inside it. So there is no actual point in using the abstract keyword there.


What is the of abatract keyword in java 1.7?

"abstract" is used for a class that is not supposed to be instantiated directly. The reason this is used is because the class is not complete; the details are supposed to be filled out in subclasses (derived classes). An abstract class will usually have one or more abstract methods; a method that is declared by name (to make sure derived classes have this method), but without a method body.


Is it possible to declare a variable with abstract keyword?

yes


What is the need to create an abstract class without abstract?

Actually there is no need & most importantly - you cannot create an abstract class without using the abstract keyword


What are abstract classin java?

An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.Ex:abstract class Parent {public abstract String getSon();public abstract String getDaughter();........//More methods that contain specific behaviour/code in them}The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.i.e., the below piece of code will not work. The code will not even compile.Parent object = new Parent();Purpose of Abstract Classes:Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.A Child Class extending the Abstract Class:public class Child extends Parent {public String getSon() {return "Sons Name";}public String getDaughter(){return "Daughters Name";}...... //Code specific to the Child class}


Can you have a final abstract class?

No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.Combining them would lead to an unusable class, so the compiler will not let this happen.


What is the purpose of the keyword private?

The private keyword is used to ensure that no other classes will be able to access (view or modify) that class member.


What is the keyword used to declare a class which cannot be instantiated?

abstract all lower case.


Which keyword can protect a class in a package from accessibility by the classes outside the package?

default