Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By default any class extends Object class. Doesn't it mean java supports multiple inheritance?

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 ?

like image 347
Kathir Avatar asked Sep 06 '25 03:09

Kathir


1 Answers

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)

like image 80
Nabin Avatar answered Sep 07 '25 21:09

Nabin