• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt regarding multiple and hybrid inheritance in java.  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Luke, thanks for the explanation, and thank you others.

Have a good day.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
JavaRanch has lots of users whom primary language is not english.
Henry


Shouldn't that be "for whom"?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, thank you for telling me, I will try to frame the question in the right way from the next time.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!