How can the Object class be a super class of subclasses?

Abhishek Keshri :

Fact 1:

Java does not support multiple inheritance.

Fact 2:

Object is a superclass of all other classes

If I have a class Parent and a class Child which is inheriting the class Parent:

class Parent {

}

class Child extends Parent {

}

In this case, how will the class Child inherit the Object class, if Java does not support multiple inheritance?

How is the relationship between these three defined?

Option 1:

enter image description here

Option 2:

enter image description here

Max Vollmer :

It's Option 2. If you define a superclass, that will be the immediate superclass of your class. If you don't define one, Object will be the immediate superclass.

class Parent {

}

class Child extends Parent {

}

is equivalent to

class Parent extends Object {

}

class Child extends Parent {

}

So, while Object is the superclass of all classes, there might be some steps in the class hierarchy before you get to Object. It's not the immediate superclass of all classes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36009&siteId=1