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
publicandabstractby default. Since Java 8 an interface may also havedefaultmethods (with a body, inherited by implementing classes) andstaticmethods. Fields in an interface are automaticallypublic static finalconstants.An interface models "can-do" (a capability), where inheritance models "is-a".
Under the Hood#
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, 600sWhy 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:
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 class | Interface | |
|---|---|---|
| A class can use | one (extends) | many (implements) |
| Instance fields (state) | yes | no (only constants) |
| Constructors | yes | no |
| Method bodies | yes | default and static methods only |
| Access modifiers on methods | any | public (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 |
| Example | Question 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#
List<String> names = new ArrayList<>(); // declared as the INTERFACE List, built as ArrayListCode 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 questionsHow many interfaces can a class implement? How many classes can it extend?
Show answerHide answer
Any number; one.
What is the diamond problem, and how does Java handle it for default methods?
Show answerHide 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()).
Give three differences between an abstract class and an interface.
Show answerHide answer
Any three: one vs many; state/constructors allowed vs not; any method bodies vs only default/static; is-a vs can-do.
Why write List<String> names = new ArrayList<>() rather than ArrayList<String> names = ...?
Show answerHide answer
Code depends on the interface, so the implementation can be changed without changing that code.