Abstract class in Java

Last Updated : 3 Jan 2026

In Java programming, abstract classes are used to define the structure and behavior of classes within an inheritance hierarchy. They act as a blueprint for other classes, where some methods may be declared without implementation. In this chapter, we will learn the concept of abstract classes including their features, advantages, and examples.

Before learning about abstract classes in Java, it is necessary to first understand the concept of abstraction.

What is Abstraction in Java?

Abstraction is the process of hiding implementation details and showing only the required functionality to the user. In other words, it displays only the essential features while hiding internal details.

For example, when sending an SMS, we type the message and send it without knowing how the message is processed or delivered internally.

Abstraction allows you to focus on what an object does rather than how it does it.

There are two ways to achieve abstraction in Java:

  • Using Abstract Class (can provide 0% to 100% abstraction)
  • Using Interface (provides 100% abstraction)

What is Abstract Class in Java?

A class declared using the abstract keyword is known as an abstract class in Java. It can contain both abstract methods (methods without a body) and non-abstract methods (methods with a body).

An abstract class provides partial abstraction. It cannot be created on its own and is used only as a parent class. It defines a common structure and shared behavior, while child classes provide their own implementations. Abstract methods do not have a body and must be implemented by the subclasses.

Points to Remember

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
Rules for Java Abstract class

Creating / Declaring an Abstract Class

An abstract class is created using the abstract keyword. It is used as a parent class and cannot be instantiated directly.

Syntax

Here is the syntax to create an abstract class:

In this example, Shape is an abstract class. It contains one abstract method area() without a body and one concrete method display() with an implementation. Any subclass of Shape must implement the area() method, while it can directly use the display() method.

Abstract Method

A method that is declared using the abstract keyword and does not have an implementation (method body) is known as an abstract method.

Example

Here is an example how an abstract method looks like:

An abstract method only declares the method signature. Its implementation must be provided by the subclass that extends the abstract class.

Abstract Class with an Abstract Method

The following example shows an abstract class that contains an abstract method. Subclasses must provide the implementation for the abstract method.

Here, Bike is an abstract class with the abstract method run(). The subclass Honda provides the implementation of run(). This shows how abstract classes define a method structure while leaving the details to subclasses.

Example

Compile and Run

Output:

running safely

More Examples of Abstract Class

Practice the following examples to better understand the concept of abstract classes and how they are used in Java.

Example 1: Shape Abstract Class

In this example, Shape is an abstract class, and its implementation is provided by the Rectangle and Circle classes. The specific implementation is usually hidden from the end user and may be provided through a factory method.

Example

Compile and Run

Output:

drawing circle

Example 2: Bank Abstract Class

This example shows how abstract classes can define a common interface for related classes, while subclasses provide their own implementation.

Example

Compile and Run

Output:

Rate of Interest is: 7 %
Rate of Interest is: 8 %

Abstract Class having Constructor, Data Members, and Methods

An abstract class in Java can contain data members, abstract methods, concrete methods, constructors, and even the main() method.

Example

The following example shows an abstract class Bike with:

  • A constructor that runs when an object of the subclass is created.
  • An abstract method run() that must be implemented by subclasses.
  • A concrete method changeGear() that can be inherited as-is.

Example

Compile and Run

Output:

       bike is created
       running safely..
       gear changed

Rules of Abstract Classes

1. If a class contains an abstract method, the class itself must be declared abstract.

Output:

compile time error

2. If a subclass extends an abstract class with abstract methods, it must either implement all abstract methods or be declared abstract itself.

Output:

Honda started

Another Real Scenario of Abstract Class

The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.

Note: If we are beginner to Java, learn interface first then see this example.

Example

Compile and Run

Output:

   I am a
   I am b
   I am c
   I am d