Lesson · Abstraction & Interfaces
Multiple inheritance of behaviour: the diamond, resolved
16 min readMultiple Inheritance of Behaviour
A class can implement any number of interfaces, so two defaults with the same signature can collide. Three rules resolve every collision, and the compiler refuses only when the choice is genuinely ambiguous.
The duck that walks and swims
Two interfaces both offer a default move(), and one class signs both. Predict what the compiler says:
interface Walker { default String move() { return "walk"; } } interface Swimmer { default String move() { return "swim"; } } class Duck implements Walker, Swimmer { } // ...now what?
Predict: two inherited bodies, one signature. The compiler cannot guess which the duck means, so it refuses: class Duck inherits unrelated defaults for move() from types Walker and Swimmer. This is the diamond problem, met not by banning multiple types but by refusing to resolve silently.
The three resolution rules
Every diamond resolves by one of three rules, in priority order. All three outcomes below were executed, not imagined:
| Situation | Rule | Outcome |
|---|---|---|
| The class (or a superclass) declares the method | Class hierarchy always beats interface defaults | The class's method runs: base |
| One interface extends the other | The most specific interface's default wins | The subinterface's answer runs: run |
| Two unrelated interfaces offer defaults | Ambiguous: the compiler refuses until you override | You choose, optionally calling Interface.super.move() for each: swim and walk |
- Read the rules as a ladder: first ask whether a class wrote the method, then whether one interface is more specific, and only then admit ambiguity.
- Overriding ends any conflict: the class's own method is checked first, and inside it
Swimmer.super.move()calls exactly the default you name. - The explicit super form also lets you compose both: swim and walk in one method, deliberately, in your chosen order.
The compiler is the tie judge, not the guesser. It resolves what has an objective answer (class beats interface, specific beats general) and stops cold at genuine ambiguity, which is a feature: the decision ends up in your code, where it can be read.
Diamond desk · two parents of behaviour
pick a caseRule 3 · ambiguous, the compiler refuses
Compile error
class Duck inherits unrelated defaults for move() from types Walker and Swimmer
Two inherited bodies, one signature, no objective winner. The compiler escalates to the class author instead of guessing; one override ends it.
All four verdicts were compiled and executed with javac and java; the error text and outputs are what the tools printed.
Why behaviour multiplies and state does not
The line the language drew: a class may inherit behaviour from many interfaces, but state from exactly one parent:
- Two parents of state would be ambiguous at the memory level: both would contribute a field named
value, and no priority rule can decide which onethis.valuemeans. - Defaults carry no state, so two conflicting ones are a resolution question, not a memory question; that is exactly why they are allowed at all.
- Constants are the exception, not the rule: interfaces may hold
static finalconstants, and a class inheriting two constants with the same name hits the same refusal until the name is qualified. - Need shared state? Reach for an abstract class or composition: the interface supplies the type, the other tools supply the storage.
Behaviour can be inherited from many places; memory cannot. The whole multiple-inheritance design in Java is that one sentence: interfaces bring types and default bodies, the single parent brings bytes.
The diamond playbook
Keeping multiple defaults civil, in order:
- Add default methods sparingly: they are for evolution and genuinely common behaviour, not for shovelling code between implementers.
- When two defaults collide, override once in the class and decide out loud: which default, or both, and in what order.
- Use
Interface.super.method()to name the exact default you want; never rely on declaration order or hope. - Prefer small interfaces with distinct method names; collisions shrink when capabilities do not overlap.
- If you keep needing shared state across implementers, reach for an abstract class (or composition) instead of stuffing state into the contract.
Every diamond has an owner: you. The language hands back the ambiguity as a compile error, and one override turns it into a decision future readers can see.
Many behaviours, one memory, three rules
Interfaces may be implemented in any number, so defaults can collide: a class method beats any default, a more specific interface beats a general one, and unrelated defaults stop at the compiler until you override and choose with Interface.super. State inherits from exactly one parent because two sets of fields cannot share one object unambiguously.
Interview angles
What is the diamond problem for interfaces, and what does Java do about it?+
Two interfaces supply the same default signature, and the implementing class would inherit both bodies. Java refuses to pick: class Duck inherits unrelated defaults for move() from types Walker and Swimmer.
The refusal is deliberate design: the language resolves objectively decidable cases and escalates genuine ambiguity to the class author, where the choice is visible and reviewable. One override ends it.
interface Walker { default String move() { return "walk"; } } interface Swimmer { default String move() { return "swim"; } } class Duck implements Walker, Swimmer { } // compile error: unrelated defaults
Common misconception: That Java bans multiple inheritance of behaviour. It banished ambiguous state, not behaviour; defaults can multiply freely as long as every collision has an owner.
Follow-up: How does a class end such a conflict?
Which of two colliding defaults wins by default?+
Neither: unrelated defaults are a compile error. When one interface extends the other, the more specific (subinterface) default wins; a method from the class hierarchy beats every default.
Priority: class methods, then the most specific interface, then ambiguity. Verified examples: a class extending a base with move() prints base; a Sprinter implementing FastWalker (which extends Walker and overrides the default) prints run.
interface FastWalker extends Walker { default String move() { return "run"; } } class Sprinter implements FastWalker { } System.out.println(new Sprinter().move()); // run
Common misconception: That declaration order or alphabetical order breaks ties. Only specificity and class precedence decide; everything else is refused.
Follow-up: Why do class methods outrank interface defaults?
What does `Interface.super.method()` do?+
It calls a specific interface's default implementation, even from a class where that interface is one of several: Swimmer.super.move() names its default exactly.
It is the tool for two jobs: picking your side in a conflict, and composing both defaults into a new one (swim and walk). It only exists inside an override, and only for a directly implemented interface.
@Override public String move() { return Swimmer.super.move() + " and " + Walker.super.move(); // swim and walk }
Common misconception: That super alone would work. Plain super looks up the class hierarchy, where interface defaults do not live; the interface must be named.
Follow-up: Why can a class not call Interface.super in an unrelated method?
Why can interfaces hold behaviour but not state?+
State from two parents is structurally ambiguous: two fields named value cannot both answer for this.value. Behaviour can be resolved by rules; storage cannot.
That is why the language allows exactly one state parent and many interface types. When shared state is genuinely needed, an abstract class or composition supplies it while the interface keeps supplying the type.
interface Bad { int value; } // error: interfaces cannot have instance fields interface Ok { int LIMIT = 100; } // a constant: static final, shared, unambiguous
Common misconception: That defaults made interfaces 'classes in disguise'. They hold code, never per-object memory; the distinction is the load-bearing one.
Follow-up: What does a class inherit when two interfaces declare constants with one name?
When would you reach for defaults instead of an abstract class?+
When the type must stay an interface (multiple implementation, cross-family adoption) and the shared code is small, stateless behaviour that most implementers want as-is.
Abstract classes still own the state-plus-template pattern: fields, constructors, and a fixed sequence with hooks. Defaults cover the interface side: evolvable contracts with convenient, optional behaviour attached.
interface Sized extends Iterable<Integer> { default boolean isEmpty() { return size() == 0; } // shared, stateless, optional int size(); }
Common misconception: That the choice is stylistic. State and constructors tip it to abstract classes; multiple types and cross-family reach tip it to interfaces.
Follow-up: What happens when a default calls an abstract method of the same interface?