Differences Between Abstract Classes and Interfaces in Java

Differences between abstract classes and interfaces

Abstract classes and interfaces in Java are both tools to achieve abstraction that allow us to declare abstract methods. We cannot create instances of abstract classes and interfaces directly, we can only do that through classes that inherit them.

Since Java 8, an interface can have default methods and static methods that contain an implementation. It makes an interface more similar to an abstract class. So, the important question is: what is the difference between interfaces and abstract classes?

Below you can see a list of some important differences between interfaces and abstract classes:

  • an abstract class can have abstract and non-abstract instance methods while an interface can have abstract or default instance methods;
  • an abstract class can extend another abstract or regular class and an interface can only extend another interface;
  • an abstract class can extend only one class while an interface can extend any number of interfaces;
  • an abstract class can have finalnon-finalstaticnon-static variables (regular fields) while an interface can only have static final variables;
  • an abstract class can provide an implementation of an interface, but an interface cannot provide an implementation of an abstract class;
  • an abstract class can have a constructor whereas an interface cannot;
  • in an abstract class, the abstract keyword is mandatory to declare a method as an abstract one, while in an interface, this method declaration is optional.

Remember, a class can extend another class, a class can also implement an interface, but an interface can only extend another interface.

The provided list of differences is by no means complete. While abstract classes and interfaces have many other distinctions, the core difference is their purpose.

Typically, interfaces are used to decouple the interface of a component (class) from the implementation, while abstract classes are often used as base classes with common fields to be extended by subclasses. The picture below demonstrates the last statement.

The typical use of abstract class vs. interface

Using abstract classes and interfaces together

Sometimes, interfaces and abstract classes are used together to make a class hierarchy more flexible. In this case, an abstract class contains common members and implements one or multiple interfaces, and concrete classes extend the abstract class and possibly implement other interfaces.

In Java, a concrete class is a class that implements or extends a generic class or interface and provides concrete, non-generic types for its type parameters.

Examine the following simplified example:

interface ManagedDevice {

    void on();

    void off();
}

abstract class AbstractDevice implements ManagedDevice {

    protected String serialNumber;
    protected boolean on;
    
    public AbstractDevice(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    protected void setOn(boolean on) {
        this.on = on;
    }
}

class Kettle extends AbstractDevice {

    protected double volume;

    public Kettle(String serialNumber, double volume) {
        super(serialNumber);
        this.volume = volume;
    }

    @Override
    public void on() {
        // do complex logic to activate all electronic components
        setOn(true);
    }

    @Override
    public void off() {
        // do complex logic to stop all electronic components
        setOn(false);
    }
}

Using both concepts in programming promotes code reusability and makes it more flexible. Use suitable abstractions or their combination when designing your class hierarchies.

As an example, you may see class hierarchies in the standard Java class library. An example of that is the collections hierarchy. It combines abstract classes and interfaces to make the hierarchy more maintainable and flexible to use in your code.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate