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

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:



Option 2:



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.

Object might not be a direct parent, but it's always a super parent.

Child extends Parent
Parent extends Object

|
V

Child [indirectly] extends Object


The JavaDoc says:


Class Object is the root of the class hierarchy. ...


If a class does not extend any other class by decalring it using the keyword extends it extends though implicit from Object.

See the Example 8.1.4-1 "Direct Superclasses and Subclasses" in the JLS, chapter 8.1.4

It shows that a class Point { int x, y; } "is a direct subclass of Object"

Further the JLS says:


The subclass relationship is the transitive closure of the direct
subclass relationship.


Therefore Object is the superclass of all classes.

Going on with the example a class ColoredPoint extends Point { int color; } "is a direct subclass of class Point.". By the transitive relationship it's a (non-direct) subclass of class Object.

Summarizing: Object is either the direct superclass or by transitive relationship the last superclass of any other class.

Answering your question:
The class Parent corresponds to the class Point and the class Child to the class ColoredPoint of the example. Only Option 2 shows this relation.

Well it is an interesting discussion. I think it will be option no 2. As if you try the below code .

public static void main(String []args){
Parent p=new Parent();
Class c= p.getClass();

Child child =new Child();
Class c1= child.getClass();
System.out.println(c.getSuperclass());
System.out.println(c1.getSuperclass());

}


You will get output as :

class java.lang.Object
class Parent


The right answer is Option 2. Any Java class inherit all parents for their parents. In other words.

Class A extends Class B
Class B extends Class C
Class C extends Class D

Class X extends A -> it means that A inherit all protected/package/public fields from B,C and D.

In your example, Class Child inherit Parent properties but also Object properties in transitive mode.

From Class Object


public class Object
Class Object is the root of the class hierarchy.
Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.


This means that every Java class has Object as root in the hierarchy, not necessarily as its immediate parent.

No multiple inheritance means in Java a class extends only 1 class; has one immediate base class. Indirectly a class can have many ancestors: Child has Parent and Object as ancestor "super" classes.

Object --> Parent --> Child
--> OtherChild

Relation: 1 --> N


The reason for avoiding multiple inheritance like in C++, was the ambiguity involved:

Pseudo code assuming multiple inheritance:

class A : Comparable
class B : Comparable

class Child : A, B {

@Override A? B?
int compareTo(Child rhs) { ... super.compareTo ? ... }
}

A a = new Child();
B b = new Child();
a.compareTo(b);


First of all, using Java 8, it is possible to accomplish Multiple inheritance using Default methods of interfaces.

Secondly, your understanding regarding Object class is correctly represented in 'Option 2'.
However, it is not multiple inheritance, rather multilevel inheritance. 'Option 1' is multiple inheritance.

Please check this link to read more about them.

Option 2, as every object derives Object.class methods

option 2.Object is a superclass of all other classes,but Object may not a dirrect superclass of a classe.

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue