All classes inherit from java.lang.Object
, although extends Object
is (generally) not written out anywhere. How is this possible?
问题:
回答1:
if you don't explicitly write extends Object
the compiler does it for you. So knowing that a class can only extend one super class, the compiler will look at the hierarchy and extend the highest super class to Object
. So every class will directly or indirectly inherit the Object
class.
The Object
class however is a special case because it doesn't extend anything.
Lastly if you were to compile a simple class and decompile it, you will see the compiler inserts extends
java.lang.Object
(or
the bytecode equivalent)
into the class
回答2:
The Object
is implicitly direct/indirect super class of all class.
From Oracle Java doc:
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
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.