Hi, I am John and pursuing graduate, my question is why Java or oops language does not use multiple inheritance? This is nightmare for me. My exam is coming soon. Please provide me a good answer

Dani AI

Generated

Short answer (exam-ready): Java does not allow a class to extend more than one class — single inheritance for classes — but a class may implement any number of interfaces. That design keeps the object model simple (no inherited instance state from multiple parents) while still letting a type have many interfaces. (download.java.net)

Why that decision was made: multiple inheritance of state and implementation brings real problems — duplicated fields, constructor/initialization ambiguity, and method-resolution ambiguity (the “diamond” issues). Java’s solution is to separate “state/implementation” (classes) from “type/contract” (interfaces). Interfaces historically had no implementations, so they avoided the ambiguity; Java’s designers documented this tradeoff. (docs.oracle.com)

What changed with Java 8: interfaces can have default methods (method bodies). That re-introduces method-resolution questions, so the language defines clear rules: a concrete class or superclass method always wins over an interface default; if two interfaces provide defaults the most specific (subinterface) wins; if ambiguity remains the implementing class must override and choose. You can explicitly call a particular default implementation with InterfaceName.super.method(...). Example:

interface A { default void hello() { System.out.println("A"); } }
interface B { default void hello() { System.out.println("B"); } }
class C implements A, B {
    @Override
    public void hello() {
        A.super.hello(); // explicitly pick A's default
    }
}

See the language rules and the Java SE 8 tutorial for details. (oracle.com)

Quick practical notes: mention the single-class-extends rule, that interfaces enable multiple-type inheritance, and that default methods (since Java 8) require explicit resolution when conflicting — those three points answer the exam question concisely. For design work, prefer composition (delegate to contained objects) or small focused interfaces rather than trying to simulate multiple class inheritance. (docs.oracle.com)

Recommended Answers

All 4 Replies

Current versions of Java do support multiple inheritance from interfaces. So your question is in error.

commented: That's what I thought. Then again they tagged the discussion with asp.net and left out Java version in use. I have a legacy app on (gasp) 1.8. +15

A class can implement any number of interfaces but can extend only one class. Multiple inheritance is not supported because it leads to deadly diamond problem. However, it can be solved but it leads to complex system so multiple inheritance has been dropped by Java founders.

Not quite true any more. SInce the introduction of lambdas Java has "default methods" in interfaces, which are inherited by any class that implements the interface. And yes, because you can implement multiple interfaces, this leads to exactly the same diamond problem as multiple inheritance from classes. There are new rules to determine what to do when a class inherits two methods with the same signature from different interfaces, but IMHO its a bit of a dog'e breakfast. I think it was a situation forced on the designers as a necessary cost of implementing lambdas in a totally backwards-compatible way.
It reminds me of the introduction of generics in 1.5. The implementation is a compromised mess because it had to be crowbarred into existing Java without breaking anything. Ditto Optionals. I'm sure they would love to start again with a clean sheet, or at least the option to be incompatible with some existing code, but then maybe they should just use Swift. :)

one class extends two superclasses or base classes but in Java, one class cannot extend more than one class simultaneously. At most, one class can extend only one class.

Therefore, To reduce the ambiguity, complexity, and confusion, Java does not support multiple inheritance through class. Let's understand with a simple scenario of why java does not support multiple inheritance through the class.

Consider a scenario from the above diagram where there are three classes class A, class B, and class C. Class C extends two parent classes such as class A and class B. Suppose class A and class B have the same method msg() (say) having different implementations. For more details: Types of inheritance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.