Junior — Middle
Is it possible to implement multiple inheritance in Java?
sobes.tech AI
Answer from AI
In Java, multiple inheritance of classes is not directly supported, meaning a class cannot inherit from more than one class. This is done to avoid problems such as the "diamond problem".
However, multiple inheritance can be achieved through interfaces. A class can implement multiple interfaces, which allows for a similar effect of multiple inheritance of behavior.
Example:
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
Thus, multiple inheritance in Java is implemented through multiple interface implementations, not through class inheritance.