From the Oracle documentation page about multiple inheritance type,we can find the accurate answer here.
Here we should first know the type of multiple inheritance in java:-
- Multiple inheritance of state.
- Multiple inheritance of implementation.
- Multiple inheritance of type.
Java "doesn't support the multiple inheritance of state, but it support multiple inheritance of implementation with default methods since java 8 release and multiple inheritance of type with interfaces.
Then here the question arises for "diamond problem" and how Java deal with that:-
In case of multiple inheritance of implementation java compiler gives compilation error and asks the user to fix it by specifying the interface name.
Example here:-
interface A {
void method();
}
interface B extends A {
@Override
default void method() {
System.out.println("B");
}
}
interface C extends A {
@Override
default void method() {
System.out.println("C");
}
}
interface D extends B, C {
}
So here we will get error as:-
interface D inherits unrelated defaults for method() from types B and C interface D extends B, C
You can fix it like:-
interface D extends B, C {
@Override
default void method() {
B.super.method();
}
}
- In multiple inheritance of type java allows it because interface doesn't contain mutable fields and only one implementation will belong to the class so java doesn't give any issue and it allows you to do so.
In Conclusion we can say that java doesn't support multiple inheritance of state but it does support multiple inheritance of implementation and multiple inheritance of type.