Open In App

Abstract Class in Java

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
282 Likes
Like
Report

In Java, abstract class is declared with the abstract keyword. It may have both abstract and non-abstract methods(methods with bodies). An abstract is a Java modifier applicable for classes and methods in Java but not for Variables. In this article, we will learn the use of abstract classes in Java.

What is Abstract Class in Java?

Java abstract class is a class that can not be instantiated by itself, it needs to be subclassed by another class to use its properties. An abstract class is declared using the "abstract" keyword in its class definition.

Illustration of Abstract class

abstract class Shape 
{
int color;
// An abstract function
abstract void draw();
}

In Java, the following some important observations about abstract classes are as follows:

  1. An instance of an abstract class can not be created.
  2. Constructors are allowed.
  3. We can have an abstract class without any abstract method.
  4. There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final  or in simpler terms final method can not be abstract itself as it will yield an error: "Illegal combination of modifiers: abstract and final"
  5. We can define static methods in an abstract class
  6. We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract
  7. If a class contains at least one abstract method then compulsory should declare a class as abstract 
  8. If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method

Examples of Java Abstract Class 

1. Example of Abstract Class that has Abstract method

Below is the implementation of the above topic:


Output
avinash
21
222.2

2. Abstract Class having constructor, data member, and methods

Elements abstract class can have

  • data member
  • abstract method
  • method body (non-abstract method)
  • constructor
  • main() method.

Below is the implementation of the above topic:


Output
Learning Subject
C , Java , C++
Preparing Right Now!

Properties of Abstract class

Let us elaborate on these observations and do justify them with help of clean java programs as follows.

Observation 1

In Java, just like in C++ an instance of an abstract class cannot be created, we can have references to abstract class type though. It is as shown below via the clean Java program.

Example 


Output
Derived fun() called

Observation 2

Like C++, an abstract class can contain constructors in Java. And a constructor of an abstract class is called when an instance of an inherited class is created. It is as shown in the program below as follows: 

Example:


Output
Base Constructor Called
Derived Constructor Called
Derived fun() called

Observation 3

In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated but can only be inherited. It is as shown below as follows with help of a clean java program.

Example:


Output
Function of Base class is called

Observation 4

Abstract classes can also have final methods (methods that cannot be overridden)

Example:


Output
Base fun() called

Observation 5

For any abstract java class we are not allowed to create an object i.e., for an abstract class instantiation is not possible. 

// Java Program to Illustrate Abstract Class

// Main class
// An abstract class
abstract class GFG {

    // Main driver method
    public static void main(String args[])
    {

        // Trying to create an object
        GFG gfg = new GFG();
    }
}

Output:

abstract class

Observation 6

Similar to the interface we can define static methods in an abstract class that can be called independently without an object. 


Output
Geeks for Geeks

Observation 7

We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract


Output
Inside abstract method implementation

Observation 8

If a class contains at least one abstract method then compulsory that we should declare the class as abstract otherwise we will get a compile-time error ,If a class contains at least one abstract method then, implementation is not complete for that class, and hence it is not recommended to create an object so in order to restrict object creation for such partial classes we use abstract keyword.


Output
Hello

Observation 9

If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method.


Output
Inside m1
Inside m2
Inside m3

In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract. 


Conclusion

Points to remember from this article are mentioned below:

  • An abstract class is a class that can not be initiated by itself, it needs to be subclassed by another class to use its properties.
  • An abstract class can be created using "abstract" keywords.
  • We can have an abstract class without any abstract method.

Abstract Class in Java
Visit Course explore course icon
Next Article

Similar Reads