All classes inherit from java.lang.Object
, although extends Object
is (generally) not written out anywhere. How is this possible?
所有类都继承自java.lang.Object,尽管extends(通常)不会在任何地方写出。这怎么可能?
2 个解决方案
#1
23
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.
如果您没有显式编写extends Object,编译器会为您执行此操作。因此,知道一个类只能扩展一个超类,编译器将查看层次结构并将最高的超类扩展为Object。因此每个类都将直接或间接继承Object类。
The Object
class however is a special case because it doesn't extend anything.
然而,Object类是一种特殊情况,因为它不会扩展任何东西。
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
最后,如果您要编译一个简单的类并对其进行反编译,您将看到编译器将java.lang.Object(或等效的字节码)扩展到类中
#2
5
The Object
is implicitly direct/indirect super class of all class.
Object是所有类的隐式直接/间接超类。
From Oracle Java doc:
来自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.
Excepting Object没有超类,每个类都有一个且只有一个直接超类(单继承)。在没有任何其他显式超类的情况下,每个类都隐式地是Object的子类。
#1
23
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.
如果您没有显式编写extends Object,编译器会为您执行此操作。因此,知道一个类只能扩展一个超类,编译器将查看层次结构并将最高的超类扩展为Object。因此每个类都将直接或间接继承Object类。
The Object
class however is a special case because it doesn't extend anything.
然而,Object类是一种特殊情况,因为它不会扩展任何东西。
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
最后,如果您要编译一个简单的类并对其进行反编译,您将看到编译器将java.lang.Object(或等效的字节码)扩展到类中
#2
5
The Object
is implicitly direct/indirect super class of all class.
Object是所有类的隐式直接/间接超类。
From Oracle Java doc:
来自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.
Excepting Object没有超类,每个类都有一个且只有一个直接超类(单继承)。在没有任何其他显式超类的情况下,每个类都隐式地是Object的子类。