Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why java doesn't support multiple inheritance?  RSS feed

 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why java doesn't support multiple inheritance? I was asked this question in an interview. I said that java supports the use of interfaces, but the interviewer wasn't pleased. So can u plzz tell me whats the exact reason?

Thx in advance.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is asked here (and elsewhere) with almost clockwork regularity. A quick search therefore should turn up a multitude of answers very quickly.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx Jeroen for your prompt reply. Surely I'll do a search.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see The singly rooted hierarchy topic in Thinking in Java.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx marc for the valueable link, but actually I've already downloaded thinking in java from the web, as you know its free and its no doubt one of the best books. But still I'm not satisfied with this topic. It's still not clear to me what extra facility java brings by avoiding multiple inheritance? May be thats bcoz I'm a little stupid, take a little more time to grab things

But thx anyway for your time.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple inheritance can be very unsafe. For example, if the programmer doesn't know what he is doing, he may create a class that extends lets say from a cat and a dog class! So now the new animal is a cat and a dog at the same time! Which is clearly impossible and a dangerous situation. So you may end up with a corrupt class.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps that could be the reason. I think Multiple inheritance like Pointers are among the complex & error-prone subjects of C++ that's why Java creators avoided it. Although I'm not sure of it.

Thankz Petros.
 
Simone Cittadini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's something about the "Diamond of Death"

googling will be better than me trying to explain it
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's also the problem where you inherit from two classes that happen to have a method with the same signiature.

both the Dog and Cat class have a speak(int counter) method, where you pass in an integer for how many times the critter barks or meows.

what happens in your bizare frankenstein class when you call speak(2)? should the creature bark or meow?

now, this is not an insurmountable issue, but it does cause some headaches.
[ July 10, 2006: Message edited by: fred rosenberger ]
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice example. You are right fred, how do I know if user calls speak(2) which one he wants--->cat or dog?
So, forget abt it....use interfaces....saves a lotta headache.

Perhaps the next big add for a java beginner who comes from a c++ world should be:-
Hey, u have migrane? come 2 Duke's world...he has the pain balm

Or even
Hey u have nightmares like pointers are after u? forget it, come to the java way...its a smooth ride n after that a cup of hot coffee
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry fellows, accidentally I made the same post appears three times. It was my mistake....I do apologize for that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i removed two of them, just to tidy up.

FYI, you can remove any post you have made yourself. click the little icon that looks like a paper and pencil, to the right of the timestamp of your post. this lets you edit your post. on the edit screen is a "delete post" checkbox.
 
ak pillai
author
Ranch Hand
Posts: 288
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:

FYI, you can remove any post you have made yourself. click the little icon that looks like a paper and pencil, to the right of the timestamp of your post. this lets you edit your post. on the edit screen is a "delete post" checkbox.


Be careful, though - deleting the very first post in a thread deletes the whole thread!
 
kethare eswaran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in C++ we use multiple inheridane there is al little disadvantage

Conside
Class A
{
---
---
---
}

now u inheride the class A to multiple class

Class B : Class A
{
}
class C : Class A
{
}

now the class A is inheride to both B and C

suppouse u want to inheride the Classes B and C to another class

Class D :Class B,Class C
{
}

in above the duplication is occur.

to overthis we dont use multiple inheridance
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!