File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Multiple Inheritance in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple Inheritance in Java" Watch "Multiple Inheritance in Java" New topic
Author

Multiple Inheritance in Java

Subhendu Dash
Greenhorn

Joined: Mar 24, 2011
Posts: 18
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

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...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Subhendu Dash
Greenhorn

Joined: Mar 24, 2011
Posts: 18
@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

Joined: Sep 27, 2012
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38350
    
  23
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 28, 2008
Posts: 5575

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
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Inheritance in Java