Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance in Java since All classes extend from Object class? [duplicate]

I have a simple question:

If I declare a class A - means this class in implicitly inheriting from Object Class.

Now If class B inherits from class A

is this Class B also not Inheriting from Object class ?

If yes, does that mean writing the keyword 'extends' some how override the implicit inheritance ( from class Object) ?

like image 407
GV1234 Avatar asked Dec 16 '13 16:12

GV1234


2 Answers

All classes extend form Object, either implicitly or explicitly, directly or indirectly, the whole class hierarchy in Java ends up pointing at Object, which is at the root. For instance, when you write this:

public class MyClass extends Object {

Is exactly the same as this:

public class MyClass {

And if we have this:

public class MySubClass extends MyClass {

Then MySubClass extends from MyClass which extends from Object. It's a transitive inheritance relationship, and it occurs in only one direction: at no point in the hierarchy it will be possible that a single class extends from more than one class - that's why we say that in Java we have single-inheritance (as opposed to: multiple-inheritance.)

like image 130
Óscar López Avatar answered Oct 15 '22 18:10

Óscar López


A inherits from Object and B inherits from A. So you have 3 levels of inheritance (2 explicit). There is no multiple inheritance here.

`Object`
    |
   `A`
    |
   `B`
like image 39
Adam Arold Avatar answered Oct 15 '22 16:10

Adam Arold