0

"Why multiple inheritance is not possible in java ?" is any different to the question "Why multiple inheritance is not supported in java ?" or both are inter related . I know this has already been answered , just looking for a difference if any not the answer to the why.

Hmahwish
  • 2,222
  • 8
  • 26
  • 45
  • (in other words, same thing. Not possible because it's not allowed.) – awksp Jun 16 '14 at 05:18
  • Kind of a meta question?! But to help you out, both questions are the same. – Thomas Uhrig Jun 16 '14 at 05:19
  • @AbimaranKugathasan Not quite the right duplicate, in my opinion; that question just addresses alternative ways of achieving multiple inheritance besides interfaces, and doesn't go into *why* – awksp Jun 16 '14 at 05:20
  • 1
    Multiple inheritance is not possible because it is not supported. The reason(s) why it is not supported have been clearly mentioned in the original question (of which this seems to be a duplicate) – TheLostMind Jun 16 '14 at 05:23
  • Why would the questions be different? "not possible" clearly implies "not supported" and vice versa. Swapping a word doesn't change the question. – awksp Jun 16 '14 at 05:28

1 Answers1

2

Both are same question.

If it is possible, we may end up with famous Diamond death problem.

The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.

In a nutshell, the problem is that if a class extended two other classes, and both superclasses had, say, a doStuff() method, which version of doStuff() would the subclass inherit?This issue can lead to a scenario known as the "Deadly Diamond of Death,"

Because of the shape of the class diagram that can be created in a multiple inheritance design. The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond."

So in Java for simplicity, you have one and only one base class. Searching for the base class is a linear search from top to bottom, simple, fast and efficient.

But multiple inheritance is useful, it's conceivable that the same class may want to represent itself differently to different clients. That is done by using interfaces. An interface is just like a base class, but carries no data and no methods.

When you implement an interface you have to supply all the members it needs, which is simple to understand. When the computer casts to the interface all it needs to do is start from the type object and search upwards through the base classes looking for one that implements the interface.

Interfaces do 95% of the work multiple inheritance for 10% of the brain space and 15% of the CPU space. The 5% that interfaces can't do that multiple inheritance can may be simply implemented by composition.

That is why its not supported.

Sanjay Rabari
  • 2,091
  • 1
  • 17
  • 32