Java · OOP, JVM and backend craft

When would you use an interface versus an abstract class?

Mediuminterfacesabstract classesdesignUpdated Oct 2026
Quiz this track

Short answer

An interface declares a contract and supports multiple inheritance of type. An abstract class can hold state, constructors and partial implementations, but a class can extend only one.

Interfaces define capabilities that unrelated classes can implement. Abstract classes define a partial implementation with shared state, protected helpers and a template method. Since Java 8 interfaces can have default and static methods, and since Java 9 private methods, but they still cannot hold instance fields.

java
public interface PaymentMethod {
    void pay(long amount);

    default String currency() {
        return "INR";
    }
}

public abstract class BasePayment implements PaymentMethod {
    protected final String merchantId;

    protected BasePayment(String merchantId) {
        this.merchantId = merchantId;
    }
}
The interface defines the contract. The abstract class adds shared state and construction.

Choosing between them

  • Use an interface when callers only need a capability or contract
  • Use an abstract class when implementations share state or a fixed algorithm skeleton
  • Prefer interfaces for API boundaries and test seams
  • Use an abstract class when you need protected members or constructor logic

A class can implement many interfaces but extend only one class. That makes interfaces the more flexible extension point. If a design needs both, define an interface for the contract and an abstract base class as an optional implementation aid.

Default methods can create diamond conflicts when two interfaces provide the same signature. The compiler forces the class to resolve it, which is usually a sign that the interface hierarchy is too clever.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Can an interface have state?
  2. 2What is a default method for?
  3. 3How do you resolve a default method conflict?
  4. 4Why is an abstract class a poor choice for a public API contract?

Say it in the interview

An interface is a contract and supports multiple implementation, so I use it at API boundaries and for capabilities. An abstract class can hold shared state, constructors and a partial implementation, which suits a template method or a family of related types. A class can implement many interfaces but extend one class, so interfaces are more flexible. When I need both, I put the contract in an interface and offer an abstract base as an optional helper.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.