3

Everybody knows that in java we can only extend "ONE" class.

But for the sake of understanding:

  1. Any Java class implicitly extends java.lang.Object
  2. If class A extends class B, wouldn't class A extend both class B and java.lang.Object implicitly ?

In such a case we are extending two classes by default.

Why is it allowed if Java doesn't support multiple inheritance ?

Eran
  • 387,369
  • 54
  • 702
  • 768
Kathir
  • 2,733
  • 12
  • 39
  • 67
  • You're misunderstanding what is meant by "multiple inheritance". Multiple inheritance means a class directly extends multiple classes. This can lead to issues like the diamond issue, which cannot occur with single inheritance. – Dave Newton Sep 15 '17 at 11:22

4 Answers4

3

No, Java prevents a class from directly extending more than one super class. Class A can extend a class B which extends class C. This is still single inheritance. All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class), which is either Object or some other class.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • If directly extending is not possible, is there a way to write a class which can be indirectly extended like java.lang.Object and make the code to extend as many classes like we want as any java class extends java.lang.Object – Kathir Oct 23 '14 at 12:53
  • @Kathir Each class can have many direct sub-classes, but only one direct super-class. So if A extends B which extends C, A has a single direct super class (B), and two in-direct super-classes (also called ancestors) - C and Object. – Eran Oct 23 '14 at 12:58
  • class a by default extend java.lang.Object - class b by default extend java.lang.Object...Now when we say class a extends class B - how the collision is handled in Java or how it works internally ? – Kathir Oct 23 '14 at 13:01
  • @Kathir If you don't specify which class A extends, it would directly extend Object by default. If, however, A extends some other class, A wouldn't directly extend Object. – Eran Oct 23 '14 at 13:03
  • Assume we have created two classes. class a and class b. as per java spec, any class extends java.lang.Object by default. so it means class a implicitly extends java.lang.Object and class b extends java.lang.Object implicitly. so when we say class a extends class b did not we say class a extends two classes???\ – Kathir Oct 23 '14 at 13:24
  • @Kathir No, if a extends b, a doesn't extend Object directly. – Eran Oct 23 '14 at 14:32
3

That would be a multilevel inheritance. You are mistaking multiple to multilevel.

A->B->C //This is multilevel inheritance which you are talking about

Multiple inheritance is like (which is not possible in java)

     A
   |   |
   B   C

Java doesn't support multiple inheritance that makes any ambiguous cases to fade away. But careful implementation of implement keyword for implementing does give feel of multiple inheritance

Conclusion:

Class A can extend a class B which extends class C. This is still single inheritance. All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class)

Nabin
  • 11,216
  • 8
  • 63
  • 98
0

Whenever class A extends class B, class A no longer extends Java.lang.Object, but however: Since every class extends Java.lang.Object as you said earlier, then class B would extend Java.lang.Object and therefore class A is still a subclass of that particular class.

Darawan
  • 15
  • 8
  • so it means class A extends B as well as java.lang.Object as well. – Kathir Oct 23 '14 at 12:57
  • Yes, but still not in the way you thought. Because in-order for this to happen, class B must extend Java.lang.Object, if class B does extend java.lang.Object then indeed class A is a subclass of both those classes. – Darawan Oct 23 '14 at 13:03
0

"Implicitly extends Object by default" means that if you don't see the extends keyword in the class declaration it "invisibly but directly" extends Object. If you see the extends keyword, the class does not directly extend Object, but the class mentioned in the extends clause. Now you have to traverse that hierarchy and at one point, you'll find a parent class with no "extends", and there the implicit inheritance strikes again.

All classes in a hierarchy transitively extend Object, but only the root does it directly. Transitively because all subclasses inherit all the traits of their parents, including their parent classes and implemented interfaces.

hiergiltdiestfu
  • 2,339
  • 2
  • 25
  • 36