I was reading about the diamond problem in case of default interfaces, and the documentation says that if there is an interface A with default method m(), which is extended by two other interfaces B & C both having there own default method m(), and now suppose we have a class D that implements both B and C, then class D needs to have it's own implementation of the method m(), otherwise compiler will through exception.
interface A {
default void m() {
System.out.println("Interface A");
}
}
interface B extends A {
default void m() {
System.out.println("Interface B");
}
}
interface C extends A {
default void m() {
System.out.println("Interface C");
}
}
//allowed multiple inheritance when D gives
//it's own implementation of method m()
//else compilation error
class D implements B, C {
public void m() {
System.out.println("Class D");
}
}
If we go by the same logic, then why JAVA hasn't resolved the diamond problem in case of classes as well, and may be then we could have extended more than one classes.
interface A {
default void m() {
System.out.println("Interface A");
}
}
class B implements A {
public void m() {
System.out.println("Interface B");
}
}
class C implements A {
public void m() {
System.out.println("Interface C");
}
}
//multiple inheritance not allowed even when D gives
//it's own implementation of method m()
//still getting compilation error
class D extends B, C {
public void m() {
System.out.println("Class D");
}
}