Abstract Classes and Methods in Java

Abstract class : A class that has abstract qualifier in its declaration is called abstract class. It can have both abstract and concrete methods.


Abstract method : A method that has been declared but not implemented is called an abstract method. abstract keyword is used to declare an abstract method.


1.1 Features of Abstract Classes

  • An abstract class can never be instantiated. But it can be used to create object references.
  • An abstract class cannot be declared static or final.
  • A class that contains an abstract method must be declared abstract.
  • A class that extends an abstract class, must implement all the abstract methods or it must be declared abstract itself.
  • An abstract class can have a mixture of abstract methods and concrete methods. Note that it can have only abstract methods or only concrete methods.

1.2 Features of Abstract Methods

  • An abstract method has no implementation, it just has the method signature and no method body.
  • An abstract method must be overridden before it can be used.
  • The enclosing class of an abstract method must be declared abstract.
  • An abstract method must not be declared static or final.

2. Uses of abstract Classes and Abstract Methods

  • It can be used to define a generic interface for all the classes that subclass it.
  • It can be used to achieve runtime polymorphism.

Output :

 


3. More Examples

3.1 Example:

A class that extends an abstract class, must implement all the abstract methods or it must be declared abstract itself.

Output :

 


3.2 Example

An abstract class can have a mixture of abstract methods and concrete methods. Note that it can have only abstract methods or only concrete methods.

Output :

 


3.3 Example

  • Abstract methods cannot be declared final because a final method cannot be overridden. So this declaration is illegal:
  • An abstract class cannot be declared final because a final class cannot be extended while an abstract class has to be extended in order to create concrete objects. So this declaration is illegal:
  • An abstract class or method cannot be declared static because  “abstract” means: “Implements no functionality”, and “static” means: “There is functionality even if you don’t have an object instance”. And that’s a logical contradiction. See this for more details.

Conclusion:

  • An abstract class or method cannot be declared static or final.
  • But an abstract class can have static and final methods. It can have constructors as well. Let’s see an example:

Output :

 


4. An abstract Class can implement an Interface

An abstract class can implement an interface like any other concrete class. It can choose to implement all the methods of the interface or some methods of the interface or it can implement none of the methods of the interface. In that case its subclass has to implement rest of the methods.

Output :

 


5. Miscellaneous Points

Interfaces provide functionalities similar to abstract classes. But they are different from classes in a number of ways. Look this article to know more about them.