aspose file tools*
The moose likes Beginning Java and the fly likes Question on multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on multiple inheritance" Watch "Question on multiple inheritance" New topic
Author

Question on multiple inheritance

Nandu Vajjala
Greenhorn

Joined: Nov 12, 2005
Posts: 8
Java unlike C++ does not support multiple inheritance.
A subclass can extend only one super class.

eg.
class A extends B {
}
Here Class A is allowd to extend only one class.

But again all java classes by default are subclasses of class Object.
Hence in the above example class A is extending class B as well as class Object.

Are'nt these two statements contradicting each other?
How can we justify the statement that java classes can extend only one class?
Can some one please clarify?

Thanks
Anand
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

B is extending Object; A is directly extending only B.

In C++, you could say:

class A: public B, public C ...

so that A could directly extend both B and C. You can't do that in Java.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The difference is that you can only extend one class directly. However, in an inheritence hierarchy, you can indirectly inherit as many classes as you want. In this situation, A only directly extends B but indirectly extends Object. This is because B directly extends Object.

Typically we can think of these relationships as parent, grandparent, great-grandparent, etc. In Java, you can only have ONE parent. In C++, you can have more than one direct parent.

Layne


Java API Documentation
The Java Tutorial
Nandu Vajjala
Greenhorn

Joined: Nov 12, 2005
Posts: 8
Little confusing.
If class A extends class B -> Class B is the parent of class A and class Object is the grand parent.

But If Class A is not extending class B(is the super class) -> class Object is the parent of class A.

Huhhh..any way i could visualise a vague picture of what is happening here.

So, Only at the run time java knows who is a parent and who is a grand parent.
[ January 14, 2006: Message edited by: Nandu Vajjala ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Nandu Vajjala:
Little confusing.
If class A extends class B -> Class B is the parent of class A and class Object is the grand parent.

But If Class A is not extending class B(is the super class) -> class Object is the parent of class A.

Huhhh..any way i could visualise a vague picture of what is happening here.

So, Only at the run time java knows who is a parent and who is a grand parent.

[ January 14, 2006: Message edited by: Nandu Vajjala ]


Your description is correct. If it helps, you can imagine a tree with Object at the top, its direct children just below that, and so on.

The parent/grandparent/etc. relationships are known at compile time as well as run time. This allows the compiler to determine if code like

is valid. In this case, class B has to extend class A in order for this to compile. This relationship can be indirect (i.e. class A could be a grandparent or great-grandparent or more) and the compiler will still know that it is valid.

I hope this helps.

Layne
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It's true that all the inheritance tree is *known* at compile time, but only the parent-relationship is encoded in the class file.

As a consequence, if you have a hierarchy A extends B extends C, you could change it to A extends B extends C' without having to recompile A. A still extends B, and the rest is reassembled at runtime.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on multiple inheritance