• 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

Multiple Inheritance in Java

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very good explanation by Jesper de Jong can be found on an old topic here https://coderanch.com/t/505885/java/java/Object-class-default-break-rule.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic