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

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, learning, and sharing knowledge.