What is abstract class?
An abstract class in java is just like a normal class with a restriction that it cannot be instantiated which means that you cannot create its object.
An abstract class is not complete and its implementation is completed by sub-classes.
Note that an abstract class may or may not contain any abstract methods(explained below) but if a class contains even one abstract method, it should be declared abstract
.
Many developers question when to use abstract class. Abstract class should be used in following conditions.
-
- While creating classes we do not know the complete implementation of a class, it should be marked as abstract and let child classes provide the functionality.
- Functionality depends on sub-classes.
Example, consider a class Vehicle having a drive method. This class is extended by Car, Truck, Train classes.
Each of these would have different implementation of drive method. Hence, it is better to create this method asabstract
.
Therefore, class Vehicle will also be abstract. - If we want that our class should always be extended or sub-classed before use, it should be marked as abstract.
A method which does not have an implementation or a body is an abstract method. An abstract method can be declared in an abstract class or an interface.
If the abstract method is declared in an interface, then its implementation is provided in the class which implements the interface.
If abstract method is declared in an abstract class, then its implementation is provided in a sub-class of the abstract class.
A method declared as abstract should have the keyword abstract
in its signature and it does not have a body. That is, it does not contain curly braces({
and }
) but rather it ends with parenthesis and semi-colon to indicate that its definition is incomplete.
An abstract method is declared using syntax
<access modifier> abstract
<return type> <method name> ();
Thus to create an abstract method with name sayHello
which returns a String
and has a public
access, the syntax will be,
public String abstract sayHello();
How to create an abstract class?
An abstract class is created using abstract
keyword. Syntax to create abstract class is
<access modifier> abstract
<class name> {
…. class methods and variables
}
Abstract class example
Below is an example of an abstract class named Student which has two fields, one abstract method and one non-abstract method
public abstract class Student { private String studentName; private int class; /** * Method which is non-abstract */ public void display(){ System.out.println("Student name : " + studentName + ", Class = " + class); } /** * Abstract method */ public abstract void grade(); }
Below is an example of a sub-class of the above abstract class which provides an implementation of the abstract method defined in its super class.
public class StudentGrade extends Student { /** * Implementation of Abstract method */ public void grade(){ if(class > 10){ System.out.println("Senior Grade"); } else { System.out.println("Junior Grade"); } } }
Let’s tweak in
- It is not necessary for an abstract class to have abstract method. A class with no abstract method can also be declared
abstract
. - If a class has even one abstract method then it is mandatory to declare the class as
abstract
. - An abstract class can have a constructor which is called when any of its sub-classes is instantiated.
- An abstract class can have a
main
method and can be executed just like another class. - A sub-class of an abstract class inherits all the non-private fields and methods of its super-class.
- If a sub-class does not provide implementation of abstract methods of its super class then it should also be declared
abstract
. - An abstract class can implement interfaces without providing implementation for interface methods. This makes sense since the class is already abstract.
- Abstract classes can also contain
final
methods.