This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance in Java

 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't support Multiple Inheritance..a well known fact among java developers....but what about the fact that every class i create has java.lang.Object as its superclass..so if say i have a class Cat that extends Animal and by default Object..so isn't this multiple inheritance?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very good explanation by Jesper de Jong can be found on an old topic here http://www.coderanch.com/t/505885/java/java/Object-class-default-break-rule.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Animal and Object are not at the same level in the inheritance hierachy.
Cat extends Animal, which in turn extends Object.

Edit: Kemal beat me to it...
 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jelle Klap..But still Cat is a java class which also extends Object class..and in this case Animal class also..isn't it?
 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cat extends the object class through the animal class.

Animal is an Object.
Cat is an Animal.
Therefore
Cat is an Object.

When you use extends (correct me if I am wrong) you are specifically telling the Cat class to inherit from the Animal class, and not the Object class. However, through inheriting from the Animal class you inadvertently also inherit/extend from the Object class.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:@Jelle Klap..But still Cat is a java class which also extends Object class..and in this case Animal class also..isn't it?

when they talk about multiple inheritance, they mean DIRECTLY. You can't do this:



but you can do this:

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to look in the Java Language Specification for the official explanation, but it might not be easy to read. I haven’t read that bit for some time, and have forgotten what it actually says.
You will probably find that Cat extends Animal and nothing else. Not Object.
You will probably find that Animal extends Object and nothing else. Even though Object is still a supertype of Cat.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:@Jelle Klap..But still Cat is a java class which also extends Object class..and in this case Animal class also..isn't it?


The term "multiple inheritance" refers to this:


not this:


Java supports the second, which does not conform to the definition of multiple inheritance. It does not support the first, which is what multiple inheritance means.

So, when you say, "Java doesn't support multiple inheritance, but then why does it allow Z to extend Y, X, and Object?" what you're really saying is, "Java doesn't support muliple inheritance, but why does it allow this thing which has nothing at all to do with multiple inheritance?"

Note that "Java does not support multiple inheritance" is not the same as "Java does not support anything which might be called 'multiple inheritance' by some non-standard definition of the term."



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:Java doesn't support Multiple Inheritance..a well known fact among java developers...

look here .
Subhendu Dash wrote:
but what about the fact that every class i create has java.lang.Object as its superclass..so if say i have a class Cat that extends Animal and by default Object..so isn't this multiple inheritance?

I think it known as multilevel inheritance.
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Note that "Java does not support multiple inheritance" is not the same as "Java does not support anything which might be called 'multiple inheritance' by some non-standard definition of the term."




I find it interesting that this comes up a lot on the ranch -- just search for previous topics. And in every case, it is based on this exact same incorrect interpretation of what is multiple inheritence. It is almost like there is a book or web site out there that is creating this mis-information... and I am getting curious in knowing what this source is.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Jeff Verdegan wrote:Note that "Java does not support multiple inheritance" is not the same as "Java does not support anything which might be called 'multiple inheritance' by some non-standard definition of the term."




I find it interesting that this comes up a lot on the ranch -- just search for previous topics. And in every case, it is based on this exact same incorrect interpretation of what is multiple inheritence. It is almost like there is a book or web site out there that is creating this mis-information... and I am getting curious in knowing what this source is.

Henry


I think people just don't realize that "multiple inheritance" has a specific definition, and that said definition does not include the situation they're asking about. Usually just explaining that clears it up, but I've seen a couple of cases where people don't get that that definition is, by definition, correct. They seem to think that the designers of Java have overlooked this other "natural" meaning of MI, and that MI somehow inherently includes the single-branch situation, and that therefore Java does support it.

</rant>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic