| Author |
Doubt regarding multiple and hybrid inheritance in java.
|
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hi everybody, I am Chaitanya. I have a doubt regarding inheritance in java. Java will not support multiple and hybrid inheritance.
What I know till now is suppose if I am having two classes namely A and B which has a method sample() in both classes. Suppose if class C is extending class A and B there arises an ambiguity.
I know this is not possible. I am just figuring it out. Now the runtime environment gets confused if the user calls sample() method. So this is why java will not support multiple or hybrid inheritance.
Am I right?
If I am right then what about this approach where I am having two interfaces A and B having one unimplemented method a() in common. Now this code is valid.
Why does not java fall in confusion this time?
Please shed some light on my topic. Thank you all in advance.
|
Love all, trust a few, do wrong to none.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
chaitanya karthikk wrote:
Why does not java fall in confusion this time?
There is no confusion because there is only one implementation -- doesn't matter if you want version A or version B, it is the same implementation.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hi Henry I dint understand what you said. And please tell me what I quoted about multiple inheritance is correct or wrong.
Thank you in advance.
|
 |
Matthew Cox
Greenhorn
Joined: May 25, 2009
Posts: 29
|
|
chaitanya karthikk wrote:Hi Henry I dint understand what you said. And please tell me what I quoted about multiple inheritance is correct or wrong.
Thank you in advance.
What he is saying is that in your second example, the compiler/run-time will make no differentiation between interface A.a() and B.a(). Since they have the same signature, return type, and name ... they are handled in the same fashion ... invoke a()
here is an example
OUTPUT:
No other implementation, other than me!
No other implementation, other than me!
No other implementation, other than me!
The only method that could be invoked is a() ... A,B will both refer to a().
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3032
|
|
|
The key here is that if you had multiple inheritance of classes, as in your example in the first post, then you have different behaviors inherited from the different classes. In the case of multiple interfaces with the same method signature, the resulting class only has a single behavior - the behavior of the method it implemented itself. The two interfaces each say the method must exist, but don't provide any behavior, so there can be no confusion about what should be done.
|
Steve
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hi Luke, thanks for the explanation, and thank you others.
Have a good day.
|
 |
Adam Richards
Ranch Hand
Joined: Nov 03, 2005
Posts: 133
|
|
|
Tip: You'll be getter responses if you use correct terminology. "Doubt" is the wrong word here. The right word would be "question" or "confusion," not "doubt."
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
Adam, "doubt" is the word that Indian English speakers use most of the time when they mean "question". We're used to it here on the Ranch.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Jesper de Jong wrote:Adam, "doubt" is the word that Indian English speakers use most of the time when they mean "question". We're used to it here on the Ranch.
To add to that, JavaRanch has lots of users whom primary language is not english. Unless it is an egregious offense, where the technical meaning of the question is changed to mean something else, let's all shy away from being grammar police please.
Henry
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
Henry Wong wrote:
JavaRanch has lots of users whom primary language is not english.
Henry
Shouldn't that be "for whom"?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
chaitanya karthikk
Ranch Hand
Joined: Sep 15, 2009
Posts: 779
|
|
Hey guys, thank you for telling me, I will try to frame the question in the right way from the next time.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
fred rosenberger wrote:
Henry Wong wrote:
JavaRanch has lots of users whom primary language is not english.
Henry
Shouldn't that be "for whom"?
No, maybe it should be "whom's".
|
 |
 |
|
|
subject: Doubt regarding multiple and hybrid inheritance in java.
|
|
|