File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Multiple Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple Inheritance" Watch "Multiple Inheritance" New topic
Author

Multiple Inheritance

prakash pawar
Ranch Hand

Joined: Mar 11, 2010
Posts: 42
Hi All,

Please Explain

why we dont have Mutliple Inheritance in java.


Regards,
Prakash Pawar.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://en.wikipedia.org/wiki/Diamond_problem

hth
prakash pawar
Ranch Hand

Joined: Mar 11, 2010
Posts: 42
Hi Seetharaman ,

thanks for reply

But C++ do have the concept of multiple inheritance how please explain.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 29, 2008
Posts: 234

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 01, 2007
Posts: 138

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
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

Joined: Sep 29, 2008
Posts: 12617

There's only one class here--I'm guessing it's that one.
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Inheritance
 
Similar Threads
Disadvantage for the Inheritance
WA #1.....word association
Realization/Generalization?
4 key points about Oops
Field hiding