interface A {
default void m() {
System.out.println("Hello from A");
};
}
interface B extends A {
default void m() {
System.out.println("Hello from B");
};
}
interface C extends A {
}
class D implements B, C {
}
public class Main {
public static void main(String[] args) {
D obj = new D();
obj.m();
}
}
I am unable to come to a decision why do i get an output..I have modeled this problem as diamond problem.. Is This model similar to Diamond Problem ? But It works Fine in Java .....Why?
Default methods in java works like this: If no implementation was provided in the concrete class, it will default to the implementation of the method which is further down in the class hierarchy.
In your code, since
BextendsA, andCdoes not provide an implementation tom(), a class which implementsBwill default toB's implementation.