JavaAdvanced#oop#interface

What is the diamond problem and how does Java avoid it with interfaces?

The diamond problem occurs when a class inherits conflicting implementations of the same method from multiple parents. Java avoids ambiguity with multiple inheritance of classes entirely, but for interface default methods, the compiler forces the implementing class to explicitly override the conflicting method.

Example
interface A { default void greet() { System.out.println("A"); } }
interface B { default void greet() { System.out.println("B"); } }
class C implements A, B {
  public void greet() { A.super.greet(); } // must resolve explicitly
}

Related Questions

1
JavaIntermediate#serialization#keywords

What is the purpose of the transient keyword outside of serialization context — does it affect anything else?

Open
2
JavaIntermediate#interface#java8

What is the difference between an interface's default method and a static method?

Open
3
JavaAdvanced#streams#exceptions

What is the difference between checked exception wrapping in streams' lambdas — why can't you throw checked exceptions inside a lambda directly used in a stream?

Open