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;
}
}
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.
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.
"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.
yes
Actually there is no need & most importantly - you cannot create an abstract class without using the abstract keyword
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}
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.
no
no it is not possible
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.
"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.
yes
Actually there is no need & most importantly - you cannot create an abstract class without using the abstract keyword
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}
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.
The private keyword is used to ensure that no other classes will be able to access (view or modify) that class member.
abstract all lower case.
default