Interview Q&A / Object-Oriented Programming

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Why does Java not allow a class to extend two classes?

SHORT ANSWER

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

java
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: A

Without 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.

#java#inheritance#diamond-problem

Related questions

Practise more Object-Oriented Programming questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Object-Oriented Programming quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.