Why does Java not allow a class to extend two classes?
To avoid the diamond problem: if two parents define the same method and a child inherits both, it is unclear which version should run. Java blocks it and lets you combine interfaces instead.
Explain it simply
Suppose your father says "study engineering" and your mother says "study medicine", and both are equally your parent. Which advice do you follow? A class with two parents faces the same conflict when both parents define hi().
Java avoids the question by allowing only one class parent. Interfaces carry no instance state, and if two interfaces ship the same default method, the compiler forces you to choose.
Example
interface A { default String hi() { return "A"; } }
interface B { default String hi() { return "B"; } }
class C implements A, B {
public String hi() {
return A.super.hi(); // you must decide explicitly
}
}
System.out.println(new C().hi()); // prints: AWithout the override in C, the code would not compile. The ambiguity is turned into a decision you make on purpose.
What to say in the interview
"Java forbids multiple class inheritance to avoid the diamond problem and the complexity of merging state from two parents. Multiple interfaces are allowed, and any clash between default methods must be resolved explicitly in the implementing class."
Common mistakes
- Thinking the diamond problem applies to interfaces without default methods. With only abstract methods there is no conflict.