Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 5/9

Abstract Class vs Interface

Part of the Java Roadmap

In short

"When would you use an abstract class instead of an interface?" is one of the most asked Java interview questions. The honest answer is about relationship and state, not syntax.

An abstract class can hold state, constructors and shared code for closely related classes, and a class can extend only one. An interface defines a capability, can be implemented by unrelated classes, and a class can implement many.

The jaadu stands at the doorway of a big family house with a key on a string around its neck, while proudly holding up a fan of different skill badges and certificates.

Think of it like this

Family home vs skill certificates

Being born into a joint family gives you a shared house, a kitchen and a surname: lots of real things, but you belong to only one family. Certificates like a driving licence or a swimming badge are different: anyone can earn them, from any family, and you can hold many. Abstract class is the family. Interfaces are the certificates.

Watch it happen

Choosing between them

Abstract class

  • Can hold instance fields and constructors
  • Shares real code among close relatives
  • A class can extend only one
  • Models is-a: ElectricScooter is a Vehicle

Interface

  • No instance state, only constants
  • Defines a capability any class can promise
  • A class can implement many
  • Models can-do: it is Chargeable, Insurable

Start with an interface for the contract. Add an abstract class only when related types genuinely share state or code.

The big ideas

What you need to know

State is the big difference

Abstract classes can keep fields like registrationNumber. Interfaces cannot keep per-object state.

Related vs unrelated

Abstract classes suit a tight family. Interfaces let unrelated classes, like a Phone and a Scooter, share one ability such as Chargeable.

One parent, many contracts

extends works once, implements can list many. That alone often decides it.

Use both together

A common design is an interface for the contract plus an abstract base class that implements part of it.

See it in code

Walk through the code

java
1interface Chargeable {2    void charge(int minutes);3}4 5interface Insurable {6    long yearlyPremium();7}8 9abstract class Vehicle {10    private final String registrationNumber;11 12    protected Vehicle(String registrationNumber) {13        this.registrationNumber = registrationNumber;14    }15 16    String registrationNumber() {17        return registrationNumber;18    }19 20    abstract int wheels();21}22 23class ElectricScooter extends Vehicle implements Chargeable, Insurable {24    ElectricScooter(String reg) {25        super(reg);26    }27 28    @Override29    int wheels() { return 2; }30 31    @Override32    public void charge(int minutes) {33        System.out.println(registrationNumber() + " charging for " + minutes + " min");34    }35 36    @Override37    public long yearlyPremium() { return 2500; }38}

1/4Two small capabilities. A phone or a laptop could also be Chargeable.

new ElectricScooter("KA-01-EV-4321").charge(45) uses state from the abstract class and a capability from an interface.

Save yourself the pain

Common mistakes

Oops

Making everything an abstract class "just in case" there is shared code later.

Do this instead

Prefer interfaces for contracts. They keep the single extends slot free for when it really matters.

Oops

Answering in interviews that interfaces can't have any method bodies.

Do this instead

Since Java 8 they can have default and static methods, and since Java 9 private methods too. The real difference is instance state and single vs multiple inheritance.

Your mission

Design a mini vehicle hierarchy

~25 min

Quick check

Which feature can an abstract class have that an interface cannot?

Go deeper

  1. 1Write a table comparing the two on state, constructors, multiple inheritance and default methods.
  2. 2Model Vehicle as an abstract class and Chargeable as an interface in one example.
  3. 3Prepare a one-minute interview answer on when you would choose each.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge