Abstract Class in Java

Abstract class in Java is a class that is declared using abstract keyword. Abstract class can have regular methods (methods with method body) as well as abstract methods (methods without method body and having abstract specifier).

General form of abstract class in Java

When is abstract class needed

When designing your classes for an application you may have a situation where a super class can provide a general structure and common implementation for few methods while leaving implementation of methods to the sub classes where implementation differs.

The methods that have to be implemented by the sub classes are specified as abstract methods in the super class. A sub class has to provide implementation of the methods that are declared as abstract in the parent class.

General form of the abstract method is as follows-

There is no method body in the abstract methods, it is the responsibility of the sub classes to override abstract methods and provide implementation.

Abstract class in Java example

Suppose you have to design payment functionality where you have to provide functionality for cash and credit card payment and you need to capture customer details like name, address.

Here capturing customer details is common but the mode of payment is different. In this scenario you can create an abstract class with abstract doPayment() method which has to be implemented by the sub classes and keep the common code in abstract class to be used by all the sub classes.

Class used to run the code-


In the example there is an abstract class Payment which has one concrete method printCustDetails() for the common implementation and an abstract method doPayment(). Abstract method is implemented by both the child classes CashPayment and CCPayment as per their requirement. Both of these classes also call the common method to print the customer details.

Constraints when using Abstract class in Java

  1. Abstract class can’t be instantiated. In the above example where Payment is an abstract class, trying to instantiate it- Payment payment = new Payment(); results in a compile time error “Cannot instantiate the type Payment”.
  2. Though Abstract class can’t be instantiated but an object reference of abstract class can be created. This object reference can hold the child class objects. That’s how abstract class in Java helps in achieving run time polymorphism.
    In the above example you can see that the reference of the abstract class Payment is created- Payment payment; which refers to the child class objects and call the appropriate doPayment() method.
  3. If there is any abstract method in a class then a class has to be declared abstract otherwise you will get compile time error “The type must be an abstract class to define abstract methods”.
  4. A sub class extending an abstract class must implement all the abstract methods of the super class if it is not then the class should also be declared as an abstract class.
    For example-

    Here subclass is not implementing one of the abstract method display() that will result in compile time error “The type ChildClass must implement the inherited abstract method ParentClass.display()”.

    If display() method is not implemented by the ChildClass then it should also be declared abstract.

  5. A constructor in a class can’t be declared as abstract. Abstract is an illegal modifier for a constructor. Any static method also can’t be abstract.
  6. A class can either be declared abstract or final not both.

Abstract class with no abstract method

You can create an abstract class with no abstract method in it i.e. all the methods are concrete and implemented there itself. That’s one way to create a class that can’t be instantiated only inherited.


Abstract Classes Compared to Interfaces

Abstract classes in Java are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
However, with abstract classes, you can declare fields that are not static and final. With interfaces, all fields are automatically public, static, and final.
In abstract classes you can define public, protected, and private concrete methods. In interfaces all methods that you declare or define (as default methods) are public.

If a class implementing an interface does not implement all the methods of that interface then that class must be declared as abstract.

Since TestClass is not implementing both of the methods of the interface it should be declared as an abstract class otherwise you will get an error “The type TestClass must implement the inherited abstract method Payment.add(int, int)”.

That’s all for the topic Abstract Class in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.