In Java, an abstract class is a class that cannot be instantiated and is designed to be extended by other classes. It is used to achieve partial abstraction, where some methods are implemented while others are left for subclasses to define. An abstract class is declared using the abstract keyword. It may contain:
- Abstract methods (methods without a body)
- Concrete methods (methods with implementation)
- Constructors
- Instance variables
- Static and final methods
Example: Abstract Class with Partial Abstraction
abstract class Shape {
String color;
// Constructor
Shape(String color) {
this.color = color;
}
// Abstract method
abstract double area();
// Concrete method
void getColor() {
System.out.println("Color: " + color);
}
}
class Circle extends Shape {
int radius;
Circle(String color, int radius) {
super(color);
this.radius = radius;
}
double area() {
return 3.14 * radius * radius;
}
}
public class GFG{
public static void main(String[] args) {
Shape s = new Circle("Red", 5);
s.getColor();
System.out.println("Area: " + s.area());
}
}
Output
Color: Red Area: 78.5
Note: Abstract Class cannot be instantiated directly
Syntax
abstract class ClassName {
abstract void methodName(); // Abstract method
void concreteMethod() { // Concrete method
System.out.println("Method implementation");
}
}
Examples of Java Abstract ClassÂ
Example 1: Abstract Class with Only Abstract Method.
abstract class Sunstar {
abstract void printInfo();
}
// Abstraction performed using extends
class Employee extends Sunstar {
void printInfo()
{
String name = "avinash";
int age = 21;
float salary = 222.2F;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
s.printInfo();
}
}
Output
avinash 21 222.2
Example: Abstract Class with Constructor and Methods.
import java.io.*;
abstract class Subject {
Subject() {
System.out.println("Learning Subject");
}
abstract void syllabus();
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus(){
System.out.println("C , Java , C++");
}
}
class GFG {
public static void main(String[] args) {
Subject x=new IT();
x.syllabus();
x.Learn();
}
}
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.
abstract class Base {
abstract void fun();
}
// Class 2
class Derived extends Base {
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class Main {
// Main driver method
public static void main(String args[])
{
// Uncommenting the following line will cause
// compiler error as the line tries to create an
// instance of abstract class. Base b = new Base();
// We can have references of Base type.
Base b = new Derived();
b.fun();
}
}
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:
abstract class Base {
// Constructor of class 1
Base()
{
// Print statement
System.out.println("Base Constructor Called");
}
// Abstract method inside class1
abstract void fun();
}
// Class 2
class Derived extends Base {
// Constructor of class2
Derived()
{
System.out.println("Derived Constructor Called");
}
// Method of class2
void fun()
{
System.out.println("Derived fun() called");
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating object of class 2
// inside main() method
Derived d = new Derived();
d.fun();
}
}
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.
abstract class Base {
// Demo method. This is not an abstract method.
void fun()
{
// Print message if class 1 function is called
System.out.println(
"Function of Base class is called");
}
}
// Class 2
class Derived extends Base {
// This class only inherits the Base class methods and
// properties
}
// Class 3
class Main {
// Main driver method
public static void main(String args[])
{
// Creating object of class 2
Derived d = new Derived();
// Calling function defined in class 1 inside main()
// with object of class 2 inside main() method
d.fun();
}
}
Output
Function of Base class is called
Observation 4
Abstract classes can also have final methods (methods that cannot be overridden)
abstract class Base {
final void fun()
{
System.out.println("Base fun() called");
}
}
// Class 2
class Derived extends Base {
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
{
// Creating object of abstract class
Base b = new Derived();
// Calling method on object created above
// inside main method
b.fun();
}
}
}
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.Â
abstract class GFG {
// Main driver method
public static void main(String args[])
{
// Trying to create an object
GFG gfg = new GFG();
}
}
Output:

Observation 6
Similar to the interface we can define static methods in an abstract class that can be called independently without an object.Â
abstract class Helper {
// Abstract method
static void demofun()
{
// Print statement
System.out.println("Geeks for Geeks");
}
}
// Class 2
// Main class extending Helper class
public class GFG extends Helper {
// Main driver method
public static void main(String[] args)
{
// Calling method inside main()
// as defined in above class
Helper.demofun();
}
}
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
import java.io.*;
abstract class B {
// declaring inner class as abstract with abstract
// method
abstract class C {
abstract void myAbstractMethod();
}
}
class D extends B {
class E extends C {
// implementing the abstract method
void myAbstractMethod()
{
System.out.println(
"Inside abstract method implementation");
}
}
}
public class Main {
public static void main(String args[])
{
// Instantiating the outer class
D outer = new D();
// Instantiating the inner class
D.E inner = outer.new E();
inner.myAbstractMethod();
}
}
Output
Inside abstract method implementation
Observation 8
If a class contains at least one abstract method, it must be declared as abstract; otherwise, a compile-time error occurs, since the class has an incomplete implementation and object creation must be restricted.
import java.io.*;
// here if we remove the abstract
// keyword then we will get compile
// time error due to abstract method
abstract class Demo {
abstract void m1();
}
class Child extends Demo {
public void m1()
{
System.out.print("Hello");
}
}
class GFG {
public static void main(String[] args)
{
Child c = new Child();
c.m1();
}
}
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.
import java.io.*;
abstract class Demo {
abstract void m1();
abstract void m2();
abstract void m3();
}
abstract class FirstChild extends Demo {
public void m1() {
System.out.println("Inside m1");
}
}
class SecondChild extends FirstChild {
public void m2() {
System.out.println("Inside m2");
}
public void m3() {
System.out.println("Inside m3");
}
}
class GFG {
public static void main(String[] args)
{
// if we remove the abstract keyword from FirstChild
// Class and uncommented below obj creation for
// FirstChild then it will throw
// compile time error as did't override all the
// abstract methods
// FirstChild f=new FirstChild();
// f.m1();
SecondChild s = new SecondChild();
s.m1();
s.m2();
s.m3();
}
}
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.Â