• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance

 
prakash pawar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please Explain

why we dont have Mutliple Inheritance in java.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
prakash pawar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seetharaman ,

thanks for reply

But C++ do have the concept of multiple inheritance how please explain.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prakash pawar wrote:But C++ do have the concept of multiple inheritance how please explain.

google it. you will get lot of links
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

suppose multiple inheritance is allowed in java then

then which class's method would be invoked. I think to avoid this we dont have multiple inheritance in java.

Regarding C++ multiple inherritance, I hope someone else will put some light if this is the right place to discuss C++
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prakash pawar wrote:But C++ do have the concept of multiple inheritance how please explain.

When the Java programming language was originally created, the engineers at Sun wanted to create a language that was powerful, but easy to use, especially by people who know C++.

So they created a language that inherited many aspects from C++, but they also left out a number of the more complicated features of C++, one of which is multiple inheritance. One of the difficulties with multiple inheritance is the diamond problem, see the link posted by Seetharaman Venkatasamy above.

In practice, you don't really need multiple inheritance very often.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Java was created the designers looked at several other languages, including C++. They took what they thought was good, and ignored what at that time they thought would make the language too complex. This includes multiple inheritance, pointers and pointer arithmetic (references are not pointers!), operator overloading and templates, among others. They have changed their minds on templates now with generics added. Because this was done using type erasure this did not require many changes in the language; the compiler is the only one that uses this information. The others would require significant language changes.

Ah, Jesper beat me to it while I was typing this / listening to someone.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Ah, Jesper beat me to it while I was typing this / listening to someone.

Happy[rare] to see, some one beat you
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:In practice, you don't really need multiple inheritance very often.

Unfortunately, they didn't provide a good alternative for implementing common behavior.
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, I agreed to why Multiple Inheritance is not provided by Java language creators. So, what happens in the following case:







Now, here which method is going to Overridden... and magically eclipse will not show any error when we write above SubClass code.

Can you please explains, how it will fulfills the contract ?

Thanks,

Rahul
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It fulfills the contract because both interfaces define a method with the same signature, and the class implements a method with the same signature. It doesn't *matter* which interface is implemented (*not* overridden)--an interface is *just* a contract. The compiler doesn't care *which* show() it is, only that there *is* a show().
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class has a method "void show()" . Each interface is a contract that requires the class to have precisely such a method, so everything is good. When you call the method, there's only one method, so that's the one that's invoked. Don't try to make it any more complex than that -- that's all there is!
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The class has a method "void show()" .


I did not get this sentence... Which class has this method ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one class here--I'm guessing it's that one.
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:There's only one class here--I'm guessing it's that one.


Yes its name is "SubClass"... It has method "public String show()"... confused with that sentence that's why wanted to clear...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Nair wrote:
David Newton wrote:There's only one class here--I'm guessing it's that one.


Yes its name is "SubClass"... It has method "public String show()"... confused with that sentence that's why wanted to clear...



Sorry, my mistake.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic