• 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

Question on multiple inheritance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic