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

Java Multiple inheritance

Sunildatta Kulkarni
Greenhorn

Joined: Apr 06, 2005
Posts: 4
Hi,
In Java, every class is inherited from "Object" base class. So if my java class, (say, Myclass) explicitly extends a different class(say BaseClass).

class MyClass extends BaseClass {
...
}

Then isn't MyClass is inheriting both Object as well as BaseClass. If yes, then Isn't this a multiple inheritance. How does java internally handles it? Correct me if I am wrong.

Regards,
Sunil
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
It's not multiple inheritance. It would be multiple inheritance if the class hierarchy wasn't a tree (exactly one parent). In your case, the direct parent of your MyClass is BaseClass, not Object.


James Carman, President<br />Carman Consulting, Inc.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Yes, it's an unfortunate clash of terminology. When class A extends class B, it inherits all of B's fields and methods as well as those of B's parent, and B's parent's parent, and so on. However, as far as A is concerned, all of those fields and methods were inherited as a single unit directly from B. In that regard, it's single inheritence.

Multiple inheritence specifically refers to one class having two direct parents as opposed to a single parent and any number of grand-parents. In Java it would look like this:In the beginning it can seem somewhat arbitrary given that a class can implement multiple interfaces:However, implementing an interface doesn't involve the complexity of inheriting instance fields and method bodies. Instead, the interface merely provides a set of method signatures for which the implementing class must provide bodies (unless it's declared abstract).
Sunildatta Kulkarni
Greenhorn

Joined: Apr 06, 2005
Posts: 4
Thank your for your comments. I think, some where I am missing some points to understand. Let me put my doubt in different way. Lets assume I have a class,

class MyClass {
....
}

By default, MyClass is a subclass of Object class (implicitly Every class is a subclass of Object class).

Now, if,

class MyNewClass extends BaseClass {
.....
}

Then, is MyNewClass extends both BaseClass and also Object class (implicitly).

please clear my doubt.

Regards,
Sunil
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Then, is MyNewClass extends both BaseClass and also Object class (implicitly).

Yes, but it is still not "multiple inheritence". That is when one class has two or more parent classes. MyNewClass has one parent (BaseClass) and one "grandparent" (Object).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24045
    
  13

This is multiple inheritance:



It's just a matter of definition of terms. The first situation presents all sorts of nasty issues -- for example, dealing with ambiguities if class C inherits a method named foo() from both A and from B. But in the second situation, there's no ambiguity at all: C will get B's version of foo().


[Jess in Action][AskingGoodQuestions]
Paulo Pontes
Greenhorn

Joined: Dec 06, 2004
Posts: 20
A Class is a direct subclass of Object if you don't extend anything. If you extends a class the only direct superclass will be the extended one.

In your example MyNewClass extends BaseClass only, but since BaseClass extends Object, you can cast MyNewClass to an Object (what may have caused your confusion).

Hope it helps
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
I think this example will help you to understand it more clearly.


See the result of the class compiler.

C:\work\java>javap A
Compiled from "A.java"
class A extends java.lang.Object{
A();
}


C:\work\java>javap B
Compiled from "A.java"
class B extends A{
B();
}

I think now its more clear.
Sunildatta Kulkarni
Greenhorn

Joined: Apr 06, 2005
Posts: 4
Thank you all. Sanju, your complied versions of java programs cleared my doubts.

Regards,
Sunil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Multiple inheritance
 
Similar Threads
doubt on inheritance?
Mutliple inheritance in Java
an parent object's lock
multiple inheritance
multiple casting