Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Object-Oriented Programming

    Interfaces (Abstraction, Part 2)

    An interface is a pure contract: a list of methods a class promises to provide, using implements.

    Chapter 6 of 126 min readOften asked in interviews

    The Plain Picture#

    A job description: "must be able to drive; must be able to speak English". It says nothing about who you are or how you learned. Anyone who can do those things qualifies — and one person can meet several job descriptions at once (driver and translator).

    The Real Names#

    • An interface is a pure contract: a list of methods a class promises to provide, using implements.

    • A class can implement many interfaces, but extend only one class.

    • Interface methods are public and abstract by default. Since Java 8 an interface may also have default methods (with a body, inherited by implementing classes) and static methods. Fields in an interface are automatically public static final constants.

    • An interface models "can-do" (a capability), where inheritance models "is-a".

    Under the Hood#

    Java
    interface Gradable {
        int maxPoints(); // abstract: implementers must provide it
    
        default String label() {
            return "Out of " + maxPoints();
        } // default: free implementation
    }
    
    interface Timed {
        int seconds();
    
        default String label() {
            return seconds() + "s";
        }
    }
    
    class TimedQuiz implements Gradable, Timed {
        // implements BOTH contracts
        public int maxPoints() {
            return 100;
        }
    
        public int seconds() {
            return 600;
        }
    
        @Override
        public String label() {
            // must resolve the clash — see below
            return Gradable.super.label() + ", " + Timed.super.label();
        }
    }
    // new TimedQuiz().label()   →   Out of 100, 600s

    Why Java has no multiple inheritance of classes — the diamond problem#

    If a class could extend two classes that both define label() with different bodies and fields, which one does it inherit? Which copy of shared fields exists? This ambiguity is the diamond problem. Java avoids it for classes by allowing only one superclass.

    Interfaces historically had no bodies and no instance fields, so implementing many was safe. When default methods were added, the same clash became possible — so Java forces you to resolve it explicitly. Without the label() override above, the compiler says:

    Output
    error: types Gradable and Timed are incompatible;

    Gradable.super.label() picks a specific interface's version. And interfaces still can't hold instance fields, so there's never a question of duplicated state.

    Abstract class vs interface — the classic table#

    Abstract classInterface
    A class can useone (extends)many (implements)
    Instance fields (state)yesno (only constants)
    Constructorsyesno
    Method bodiesyesdefault and static methods only
    Access modifiers on methodsanypublic (private helpers allowed since Java 9)
    Models"is-a", shared code and state for closely related classes"can-do", a capability many unrelated classes can have
    ExampleQuestion with shared text, points, grade()Comparable, Runnable, Gradable

    How to choose: default to an interface for the type that other code depends on. Add an abstract class underneath only if several implementations genuinely share state and code. Many designs do both: interface Question + abstract class BaseQuestion implements Question.

    Programming to an interface#

    Java
    List<String> names = new ArrayList<>(); // declared as the INTERFACE List, built as ArrayList

    Code that depends only on List works with any list implementation; you can swap ArrayList for another without touching it. This idea is the Dependency Inversion principle (SOLID Principles) and is why interfaces are everywhere in real code — including testing, where you swap a real PaymentGateway for a fake one.

    Check yourself

    4 questions
    1

    How many interfaces can a class implement? How many classes can it extend?

    Show answer

    Any number; one.

    2

    What is the diamond problem, and how does Java handle it for default methods?

    Show answer

    Ambiguity when a class inherits the same method (and possibly state) from two parents. Java allows only one superclass, and for clashing default methods it forces the class to override and choose explicitly (X.super.method()).

    3

    Give three differences between an abstract class and an interface.

    Show answer

    Any three: one vs many; state/constructors allowed vs not; any method bodies vs only default/static; is-a vs can-do.

    4

    Why write List<String> names = new ArrayList<>() rather than ArrayList<String> names = ...?

    Show answer

    Code depends on the interface, so the implementation can be changed without changing that code.