• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt regarding multiple and hybrid inheritance in java.

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luke, thanks for the explanation, and thank you others.

Have a good day.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic