All notes
Java Development

Difference Between Abstract Class and Interface in Java

3 min read Engineering notes

Abstract classes and interfaces both define contracts for classes, but they solve different design problems. An abstract class is useful when related classes share state or common implementation. An interface is useful when different classes need to promise the same capability.

Abstract Class

An abstract class is a class that cannot be instantiated directly. It can contain fields, constructors, concrete methods, and abstract methods that subclasses must implement.

abstract class Vehicle {
    private final String registrationNumber;

    protected Vehicle(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void start() {
        System.out.println("Vehicle is starting");
    }

    public abstract void move();
}

class Car extends Vehicle {
    public Car(String registrationNumber) {
        super(registrationNumber);
    }

    @Override
    public void move() {
        System.out.println("Car is moving on the road");
    }
}

Vehicle provides shared state through registrationNumber and shared behavior through start(). Each subclass supplies its own implementation of move().

Interface

An interface defines a capability that a class agrees to provide. A class can implement multiple interfaces, which makes interfaces useful for composing behavior across otherwise unrelated classes.

interface Chargeable {
    void charge();
}

class ElectricCar extends Vehicle implements Chargeable {
    public ElectricCar(String registrationNumber) {
        super(registrationNumber);
    }

    @Override
    public void move() {
        System.out.println("Electric car is moving silently");
    }

    @Override
    public void charge() {
        System.out.println("Electric car is charging");
    }
}

Chargeable does not own the car's state. It only defines the behavior that any chargeable object must provide. A class can implement multiple interfaces:

class SmartPhone implements Chargeable, Comparable<SmartPhone> {
    @Override
    public void charge() {
        System.out.println("Phone is charging");
    }

    @Override
    public int compareTo(SmartPhone other) {
        return 0;
    }
}

Key Differences

Abstract classInterface
Represents a shared base for closely related classesRepresents a capability or contract
A class can extend only one abstract classA class can implement multiple interfaces
Can have instance fields and constructorsCannot have constructors or ordinary instance fields
Can contain abstract and concrete methodsCan contain abstract methods, default methods, and static methods
Members can use private, protected, and public accessInterface methods are public by default unless explicitly private or static
Best when subclasses share state and implementationBest when unrelated classes share behavior

Using Both Together

Abstract classes and interfaces are not mutually exclusive. A common design is to use an abstract class for shared implementation and an interface for an optional capability.

public class AbstractVsInterfaceExample {
    public static void main(String[] args) {
        ElectricCar car = new ElectricCar("KA-01-AB-1234");

        car.start();
        car.move();
        car.charge();
    }
}

Here, ElectricCar inherits common vehicle behavior from Vehicle and also promises to support the Chargeable capability.

When Should You Use Each?

Use an abstract class when:

  • The classes have a clear parent-child relationship.
  • They need to share fields, constructors, or common method implementations.
  • You want to provide a partial implementation and leave a few methods abstract.

Use an interface when:

  • You are describing a capability such as Chargeable, Runnable, or Comparable.
  • Unrelated classes may need to provide the same behavior.
  • A class needs to combine multiple contracts.
  • You want to support loose coupling and easier substitution in tests.

Summary

An abstract class answers, "What is this object fundamentally?" An interface answers, "What can this object do?" Choose an abstract class for shared identity and implementation, and choose an interface for a reusable capability or contract.