Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance and class Object

I am pretty new to OOP. We all know that Java does not support multiple inheritance; however, all Java classes inherit from Object and can also inherit from another class. Can we consider this as multiple inheritance? How does Java handle such a thing?

like image 974
Ibrahim Amer Avatar asked Dec 04 '22 10:12

Ibrahim Amer


2 Answers

It's not multiple inheritance it's multi level inheritance. Classes can extend one other class, which can extend one other class, ..., which ultimately extends Object:

A --> B --> C --> Object

Multiple inheritance would be

A ----> B 
  \
   \--> C

This means that when a method or a field is used inside A, it's looked up in A, then in B, then in C, then in Object.

With multiple inheritance, it would have to be looked up in A, then in B and C, and there could be a conflict because the same method or field could exist in both superclasses.

like image 189
JB Nizet Avatar answered Dec 29 '22 17:12

JB Nizet


That is not multiple inheritance ....That is multi level inheritance in java

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

enter image description here

like image 42
Suresh Atta Avatar answered Dec 29 '22 15:12

Suresh Atta