• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on multiple inheritance

 
Nandu Vajjala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nandu Vajjala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic