Q15: How does Java resolve the Diamond Problem with Interface default methods?

If a class implements two interfaces that declare the exact same default method signature, the Java compiler raises a compile-time error due to ambiguity. The implementing class MUST resolve the conflict explicitly by overriding the method and selecting a specific interface using InterfaceName.super.methodName() or providing custom logic.

Resolving Default Method Diamond Ambiguity
interface A { default void show() { System.out.println("A"); } }
interface B { default void show() { System.out.println("B"); } }

class C implements A, B {
    @Override
    public void show() {
        A.super.show(); // Explicitly resolves ambiguity
    }
}
💡 C resolves the collision by explicitly invoking A's default implementation.
Key Takeaways
  • Ambiguous default methods force compile-time resolution.
  • Resolve using InterfaceName.super.methodName() inside the override.
Knowledge Check

What happens if class C implements Interface A and Interface B, which both contain an identical default method signature, without overriding it?