Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Muliple inheritance  RSS feed

 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

Why multiple inheritance is not supported by JAVA?

thanks
Shubha
 
Pran Pal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because...they decided not to.

I guess it's less confusing this way...
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General (Beginner) forum.. This isnt covered in the SCJP exam.

Mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java designers copied a lot of things from C and C++ to make the transition from those languages easy. But they also had a list of things that made C/C++ too hard or error prone that they deliberately left out of Java. Multiple inheritance was in the cut list.
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although the responses are correct, there is more to the story than that. In the early 1990s, before Java was released, C++ was the dominant OO language for development, and it went through some growing pains. One of the problems compilers ran into was how to properly represent multiple inheritance that a)worked, and b)was relatively efficient. Multiple inheritance, it turned out, wasn't as big a problem inherently as "common" multiple inheritance, in which the two superclasses share a third common class. This resulted in a relationship called, believe it or not, the "diamond of death", because the class diagram for such a relationship formed a diamond, with the "common" base class at the top of the diamond.

The developer of Java (James Gosling) decided to implement much of the functionality of multiple inheritance via a new approach called an interface, eliminating much of the problem. However, it introduced a new problem...since interfaces don't have any code (only method declarations), what do you do if you need to "add" code from two different existing classes? In C++ it's easy...you just inherit from them. In Java, you have to jump through a few more hoops, but it's still possible to do.

In my experience, good design usually mitigates or eliminates the need for multiple inheritance. But every so often a problem arises for which multiple inheritance would actually make more sense that single inheritance.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya wat tom defind is absolutely right.. but the ultimate aim is to avoid name collision..

jus assume if java has multiple inheritance..



this is wat the problem with multiple inheritance. and as well u cannot force the java programmers to define different names for the methods. so avoid that too this is being handled. and in interfaces you are supposed to override all the methods. and even in interface also this happens if there are two mtd with same name and overriden..but the thing is if u have same method name in two interfaces then when u override one method then you will be able to define the body to only one of those methods. becoz two methods cant have the same name.so obviously you can define only one method of that name. and the implemented method ie. the overriden method will be called.when u create an object and call it.

i hope the explanation is far clear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!