Skip to content

Abstract Classes in Java

An abstract class in Java is a class that cannot be instantiated directly. It is used as a blueprint for other classes, often providing a base set of functionalities while leaving certain methods unimplemented for subclasses to define.

An abstract class is declared using the abstract keyword.


Key Characteristics of Abstract Classes

  1. Cannot Be Instantiated: You cannot create an object of an abstract class directly.
  2. Can Have Both Abstract and Concrete Methods:
    1. Abstract methods: Declared without implementation.
    1. Concrete methods: Fully implemented in the abstract class.
  3. Supports Inheritance: Abstract classes are meant to be extended by subclasses.
  4. Can Have Constructors: Although you can’t instantiate an abstract class, it can have constructors to initialize fields.
  5. Can Have Fields and Methods: Abstract classes can contain instance variables, static variables, and methods.

Syntax of Abstract Class

abstract class AbstractClass {

    // Abstract method (does not have a body)

    abstract void abstractMethod();

    // Concrete method (has a body)

    void concreteMethod() {

        System.out.println(“This is a concrete method.”);

    }

}


When to Use Abstract Classes

  • Use an abstract class when you want to provide a common base with shared code for related classes.
  • Use it when you have some methods that must be implemented differently in derived classes.

Example of Abstract Class

// Abstract class

abstract class Animal {

    // Abstract method (no implementation)

    abstract void sound();

    // Concrete method

    void sleep() {

        System.out.println(“Sleeping…”);

    }

}

// Subclass that extends the abstract class

class Dog extends Animal {

    // Implementing the abstract method

    @Override

    void sound() {

        System.out.println(“Dog barks”);

    }

}

// Subclass that extends the abstract class

class Cat extends Animal {

    // Implementing the abstract method

    @Override

    void sound() {

        System.out.println(“Cat meows”);

    }

}

public class Main {

    public static void main(String[] args) {

        // Animal animal = new Animal(); // Not allowed (abstract class)

        Animal myDog = new Dog();

        myDog.sound();  // Output: Dog barks

        myDog.sleep();  // Output: Sleeping…

        Animal myCat = new Cat();

        myCat.sound();  // Output: Cat meows

        myCat.sleep();  // Output: Sleeping…

    }

}


Abstract Classes vs Interfaces

FeatureAbstract ClassInterface
MethodsCan have both abstract and concrete methods.All methods are abstract by default (Java 7). From Java 8, interfaces can have default and static methods.
InheritanceSupports single inheritance.Supports multiple inheritance.
FieldsCan have instance and static variables.Can only have static final variables (constants).
ConstructorsCan have constructors.Cannot have constructors.
Use CaseUsed when classes share common behavior.Used to define a contract for classes to implement.

Benefits of Abstract Classes

  1. Code Reusability: Shared behavior can be implemented in the abstract class.
  2. Encapsulation: Keeps shared behavior and forces derived classes to implement specific functionality.
  3. Polymorphism: Allows handling of derived classes through a common base class reference.

Abstract classes are a powerful tool for creating extensible and reusable code structures in Java.