In the past (Java 7 and before), Java classes and interfaces served different roles: classes for abstracting method implementation; interfaces for abstracting object structure. However, since Java 8, interfaces can now define a method implementation using default methods. This leads to a problem known as the "diamond problem".
An interface A
with a method execute()
is extended by interfaces B
and C
containing default implementations of execute()
. If a class then implements B
and C
, there is ambiguity to which default implementation of execute()
should be run.
interface A {
public void execute();
}
interface B extends A {
default void execute() { System.out.println("B called"); }
}
interface C extends A {
default void execute() { System.out.println("C called"); }
}
class D implements B, C {
}
Given the class definitions above, when (new D()).execute()
is executed, what will be printed (if anything): "B called
", or "C called
"?