Interface vs Abstract Class in Java: Differences and When to Use Each

admin5 min read
In this tutorial
  1. Abstract classes
  2. Interfaces
  3. The key differences
  4. When to use which
  5. Sealed interfaces (Java 17)
  6. Functional interfaces and lambdas
  7. How to answer the interview question
  8. Summary

“What is the difference between an interface and an abstract class?” is one of the most common Java interview questions, and the answer changed a lot after Java 8 added default methods. This tutorial explains both, with up-to-date rules and a practical way to decide which to use.

Abstract classes

An abstract class is a class that cannot be instantiated on its own. It exists to be extended. It can have:

  • fields (including instance state),
  • constructors,
  • fully implemented methods,
  • abstract methods, which have no body and must be implemented by subclasses.
public abstract class Shape {
    private final String name;

    protected Shape(String name) {
        this.name = name;
    }

    public abstract double area();          // subclasses must implement

    public String describe() {              // shared behaviour
        return String.format("%s with area %.2f", name, area());
    }
}

public class Circle extends Shape {
    private final double radius;

    public Circle(double radius) {
        super("Circle");
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle extends Shape {
    private final double width, height;

    public Rectangle(double width, double height) {
        super("Rectangle");
        this.width = width;
        this.height = height;
    }

    @Override
    public double area() {
        return width * height;
    }
}

new Shape("x") will not compile, but new Circle(2).describe() works and prints Circle with area 12.57. Notice how describe() in the parent calls area(), which each subclass provides. This is the Template Method pattern: the abstract class defines the overall algorithm and lets subclasses fill in the steps.

Interfaces

An interface defines a contract: a set of methods that implementing classes promise to provide. A class can implement many interfaces.

public interface Payable {
    double amountDue();
}

public interface Notifiable {
    void notify(String message);
}

public class Invoice implements Payable, Notifiable {
    private final double total;

    public Invoice(double total) {
        this.total = total;
    }

    @Override
    public double amountDue() {
        return total;
    }

    @Override
    public void notify(String message) {
        System.out.println("Invoice notice: " + message);
    }
}

Interface methods are public and abstract by default. Fields in an interface are always public static final constants.

Modern interface features

Since Java 8 and 9, interfaces can also contain:

  • default methods, with a body that implementing classes inherit and may override,
  • static methods, utility methods called on the interface itself,
  • private methods, helpers shared by default methods.
public interface Discountable {
    double price();

    default double priceAfter(double percent) {
        return round(price() * (1 - percent / 100));
    }

    private double round(double value) {
        return Math.round(value * 100) / 100.0;
    }

    static Discountable of(double price) {
        return () -> price;   // Discountable has one abstract method, so a lambda works
    }
}

Discountable.of(2000).priceAfter(15) returns 1700.0.

Default methods were added so the JDK could add methods like forEach and stream to existing interfaces such as Collection without breaking every class that already implemented them. They are not meant to turn interfaces into abstract classes: interfaces still cannot hold instance state.

The key differences

  • Instantiation: neither can be instantiated directly.
  • Multiple inheritance: a class can implement many interfaces but extend only one class (abstract or not).
  • State: abstract classes can have instance fields; interfaces only have constants.
  • Constructors: abstract classes have them; interfaces do not.
  • Access modifiers: abstract class members can be protected or package-private; interface methods are public (or private helpers).
  • Purpose: an abstract class says “is a kind of”; an interface says “can do”.

When to use which

Choose an interface when:

  • You are defining a capability that unrelated classes might share: Comparable, Runnable, Payable.
  • You want callers to depend on a contract, not an implementation. This makes testing and swapping implementations easy, and it is how Spring wires most applications together.
  • A class may need to fulfil several roles.

Choose an abstract class when:

  • Several closely related classes share state and code, not just method signatures.
  • You want to enforce a fixed algorithm with customisable steps (Template Method).
  • You need non-public members or constructor logic shared by subclasses.

A common combination is to use both: an interface for the public contract and an abstract class that implements it partially to save subclasses some work. The JDK does exactly this with List (interface) and AbstractList (abstract base class).

Sealed interfaces (Java 17)

Java 17 lets you restrict which classes may implement an interface or extend a class:

public sealed interface PaymentResult permits Success, Failure { }

public record Success(String reference) implements PaymentResult { }
public record Failure(String reason) implements PaymentResult { }

Because the compiler knows every possible implementation, switch expressions can check that you have handled them all:

static String message(PaymentResult r) {
    return switch (r) {
        case Success s -> "Paid! Ref: " + s.reference();
        case Failure f -> "Payment failed: " + f.reason();
    };
}

Sealed types are a great fit for modelling a fixed set of outcomes, such as API results or states in a workflow.

Functional interfaces and lambdas

An interface with exactly one abstract method is a functional interface, and you can implement it with a lambda instead of a class:

Comparator<String> byLength = (a, b) -> Integer.compare(a.length(), b.length());
Runnable task = () -> System.out.println("Running");

Mark your own with @FunctionalInterface so the compiler stops anyone from adding a second abstract method by accident.

How to answer the interview question

A strong, concise answer:

“An abstract class is a partially implemented base class. It can hold state, constructors and protected members, and a class can extend only one. An interface defines a contract. A class can implement many, and since Java 8 interfaces can have default and static methods, but they still can’t hold instance state. I use interfaces to define capabilities and decouple code, and abstract classes when closely related classes share state and implementation.”

Then give a one-line example of each from your own experience.

Summary

  • Abstract classes share state and code among closely related classes; single inheritance only.
  • Interfaces define contracts; a class can implement many; default methods add behaviour without state.
  • Prefer interfaces for flexibility and testing; reach for abstract classes when there is real shared state.
  • Sealed interfaces and records model closed sets of types cleanly in modern Java.

Ask a question or leave a comment

Your email address will not be published. Required fields are marked *