← Back to Java

Diamond Problem in Java

JavaSeniorjava

The Question

What is the diamond problem in Java?

What a Strong Answer Covers

  • ambiguity in multiple class inheritance
  • Java disallows multiple class inheritance
  • default methods in Java 8 interfaces create similar issue
  • class wins over interface
  • more specific interface wins
  • explicit override to resolve ambiguity

Senior-Level Answer

The diamond problem is a classic multiple inheritance ambiguity that arises when a class inherits from two parents that both provide a concrete implementation of the same method, and both parents share a common superclass or interface defining that method. The shape forms a diamond in the inheritance graph: a common top, two middle nodes, and one bottom class inheriting from both.

Java avoids the classic diamond problem entirely for class inheritance because Java does not support multiple class inheritance — a class can extend only one superclass. This was a deliberate design decision, making the language simpler and less error-prone compared to C++.

However, Java 8 introduced default methods on interfaces, which brought a form of the diamond problem back. If two interfaces both declare a default method with the same signature, and a class implements both interfaces without overriding that method, the compiler refuses to compile and requires an explicit override in the implementing class.

The resolution rules are: first, a class implementation always wins over an interface default — if the class or any of its superclasses provides a concrete method, that implementation is used. Second, if resolution comes down to two interface defaults, the more specific interface wins (a sub-interface's default overrides a parent interface's default). Third, if neither rule resolves the conflict, the implementing class must override the method explicitly, and can delegate to a specific interface default using InterfaceName.super.methodName().

For example: class C implements A, B where both A and B have default void greet(). Class C must override greet() and may call A.super.greet() or B.super.greet() as needed.

Understanding this demonstrates mastery of Java 8+ interface evolution and why default methods were designed with these explicit conflict-resolution rules.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains that Java avoids the problem for classes via single inheritance, and that default methods on interfaces create a similar conflict requiring an explicit override.

3/3 — Strong Answer

Describes all three resolution rules (class wins, more specific interface wins, explicit override required), provides the InterfaceName.super.method() delegation syntax, and explains why the compiler rejects ambiguous cases.

Common Mistakes

  • Saying Java has no diamond problem at all — it does exist with interface default methods since Java 8.
  • Not knowing the InterfaceName.super.methodName() syntax for explicitly delegating to a specific default method implementation.
  • Missing the class-over-interface rule: a class method always takes priority over any interface default, which is the most important resolution rule.

Follow-Up Questions

  • If class C extends class B which implements interface A, and interface A has a default method that class B doesn't override, which implementation does C use? — B's class method wins if B provides one; otherwise the interface default from A is used. Class implementations always beat interface defaults.
  • Why did the Java designers allow default methods on interfaces despite the diamond problem risk? — To enable interface evolution without breaking existing implementations — adding methods to widely-used interfaces like Collection without forcing all implementors to update.
  • Can static methods in interfaces cause a diamond conflict? — No — interface static methods are not inherited by implementing classes and are called via the interface name, so they cannot conflict.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout