The Diamond Problem in Java — Why Java Bans It for Classes and How It's Resolved for Interfaces
Fast recall
Quick revision
Core answer
default methods from two interfaces, it must override the method and can call a specific parent version via InterfaceName.super.method(). The rule order: class wins over interface, more specific interface wins over less specific, and explicit override is required otherwise (compile error).Whether the candidate understands why Java 1.0 banned multiple inheritance of classes, how default methods in Java 8 reintroduced the possibility of a diamond, and what resolution rules the compiler applies
Claiming Java 'has no diamond problem because there's no multiple inheritance' — ignoring that interfaces with default methods can form a diamond. Forgetting the `Interface.super.method()` syntax to disambiguate
Explain the three resolution rules in order: class wins, more specific interface wins, else explicit override required. Mention C++'s virtual inheritance as the comparison point and why Java's designers chose to forbid rather than solve
Interview-ready guide
Interview answer
The diamond problem is the classic multiple-inheritance ambiguity — a type inherits the same method from two parents and the language has to decide which version wins.
How Java 1.0 handled it
- Banned multiple class inheritance entirely — a class
extendsexactly one other class - Interfaces were pure contracts with no method bodies, so no ambiguity even with
implements A, B - That eliminated the C++-style diamond by design
Why it came back in Java 8
- Default methods let you add real method bodies to interfaces without breaking existing implementors — that's how
Collection.stream()was added retroactively - A class implementing two interfaces that both provide a
defaultfor the same method creates the diamond
The three resolution rules the compiler applies
- Class always wins — a concrete method on the class (or any superclass) overrides any interface
default - More specific interface wins — if one interface extends the other, the child's default wins
- Otherwise explicit override required — unrelated interfaces both providing a default produce a compile error until you override
Disambiguation syntax
- Use
Interface.super.method()to reach a specific parent interface's body:return Friendly.super.greet(); - This keeps resolution explicit in code, not dependent on
implementsorder
Recommendation: Lean on the compiler — Java catches diamond ambiguities at compile time, so as long as you add an explicit @Override whenever the compiler complains, you can't ship a silent ambiguity. Use default methods to add library capabilities retroactively, but keep them few and focused to minimize the diamond surface.
The Diamond Problem in Java — Why Java Bans It for Classes and How It's Resolved for Interfaces
Direct Answer
The diamond problem is the ambiguity that appears when one type inherits the same method from two paths — which version runs? Java forbids multiple inheritance of classes precisely to avoid this. With Java 8 default methods in interfaces, the diamond can re-appear, so the compiler forces you to resolve it explicitly: if a class inherits conflicting default methods from two interfaces, it must override the method and can call a specific parent version via InterfaceName.super.method(). The rule order: class wins over interface, more specific interface wins over less specific, and explicit override is required otherwise (compile error).
Common Mistakes & Pitfalls
Claiming Java 'has no diamond problem because there's no multiple inheritance' — ignoring that interfaces with default methods can form a diamond. Forgetting the Interface.super.method() syntax to disambiguate
Check your understanding
Answer these without looking back at the article.
- If a class overrides a default method, can it still call the interface's default implementation? How?
- Why does Java allow multiple interface inheritance but not multiple class inheritance?
- What happens if two interfaces declare the same method but only one provides a default?
- How does C++ virtual inheritance solve the diamond problem, and why did Java reject that approach?
- Can an abstract class 'inherit' conflicting default methods from two interfaces without overriding them?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.