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:
The Object class is the superclass of all other classes in Java and a part of the built-in java. lang package. If a parent class isn't specified using the extends keyword, the class will inherit from the Object class.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Object class is the root or superclass of the class hierarchy, which is present in java. lang package. All predefined classes and user-defined classes are the subclasses from Object class.
All classes in Java by default "extend" the Object class, that's why Object is superclass of every class in Java. As per the definition of class "Object". Class Object is the root of the class hierarchy. Every class has Object as a superclass.
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
.
The documentation says:
In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
See the Example 8.1.4-1 "Direct Superclasses and Subclasses" in JLS, chapter 8.1.4
It shows that a class Point { int x, y; }
"is a direct subclass of Object"
Moreover the documentation says:
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class,
Object
. Such a class is said to be descended from all the classes in the inheritance chain stretching back toObject
.
The JLS states it short and formal:
The subclass relationship is the transitive closure of the direct subclass relationship.
Thus class Object
is the superclass of all classes.
But the documentation also says:
Excepting
Object
, which has no superclass, every class has one and only one direct superclass (single inheritance).
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 the questions:
Parent
corresponds to the class Point
and the class Child
to the class ColoredPoint
of the JLS example. Only Option 2 shows this relation.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With