Abstract class in Java

1. Introduction

A class that is declared with an abstract keyword is known as Abstract Class. It is used to achieve a certain level of abstraction.

An abstract class provides partial abstraction, whereas an interface provides full abstraction.

2. Creating an Abstract Class

To create an Abstract class, it must be declared as abstract. An abstract class may or may not contain the abstract method (a method without a body). An abstract class can have data member, abstract method, non-abstract method, constructor and even the main() method.

2.1 Example

In the above snippet, we have three classes, where Bicycle is an abstract class, Phoenix is the subclass which extends the Bicycle interface, and AbstractExample class is used to instantiate the Bicycle class.


Running the above Java application, below is the output that we see –

3. Why Abstract classes?

Below are some of the good reasons why we need abstract classes –

  • It provides a useful way to create a generic type, with common/default implementations for some of the method/behaviors, that can be used as a superclass for many subclasses.
  • It saves time and allows overriding.
  • When we want to create an abstract class, which only defines a generalized form that will be shared by all of its subclasses, leaving it to each class to implement it according to the requirements.
  • It is used when superclass is unable to create a meaningful implementation for methods, so it can declare the methods as abstract.

3.1 Yet another abstract class scenario

Assume Shape as an abstract class. Now in Shape thousands different shapes exist and each shape has different areas and sizes. So let’s declare area() as an abstract method in it as – abstract float area().

If we want to calculate areas of three different shapes (Triangle, Oval, Circle) then we create three subclasses for different shapes (Triangle, Oval, Circle) and extend the abstract class Shape in each of the subclasses, and individually provide an implementation to the area() method. Now we need to create objects in the main() method to call the subclass methods.

4. Can we instantiate abstract classes?

An abstract class, similar to interfaces, cannot be directly instantiated with the new operator. There would be no objects of an abstract class and such objects are also useless because an abstract class is not fully defined/complete. However we can create an abstract class reference and make it point to one of its subclass instance, very much similar to an interface (Section 2.1).

5. When to use abstract classes over interfaces?

Particularly when we need a part of the class to be implemented. The best example that can be used is the template method pattern. In Template design pattern, there is an abstract class that exposes a defined way to execute its methods, similar to the below snippet.

Thus, you can define the steps that will be taken when Do() is called, without knowing the specifics of how they will be implemented. Deriving classes must implement the abstract methods, but not the Do() method.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.