[英]will there be object creation of parent class if we create an object of child class?
I know that super will be called every time we create a child class object. 我知道每次创建子类对象时都会调用super。 But, what I specifically want to know is that will it load the class or will it create the object of that parent class. 但是,我特别想知道的是它会加载类还是会创建该父类的对象。
Thanks in advance. 提前致谢。
You have a single object. 你有一个对象。
As a class is instantiated, it invokes first the constructor of the parent class. 在实例化类时,它首先调用父类的构造函数。 And for each parent class, it follows the same logic : it invokes first the constructor of the parent class. 对于每个父类,它遵循相同的逻辑:它首先调用父类的构造函数。
Suppose : A
extends B
extends C
. 假设: A
扩展B
扩展C
new A()
results to the B
constructor invocation that invokes itself the C
constructor. new A()
导致B
构造函数调用,它自己调用C
构造函数。
As C
then B
constructor invocations are returned, the A
constructor can go on and the instance A
is created. 作为C
然后返回B
构造函数调用, A
构造函数可以继续并创建实例A
But, what I specifically want to know is that will it load the class or will it create the object of that parent class. 但是,我特别想知道的是它会加载类还是会创建该父类的对象。
Similarly to the constructor execution, before loading a class, its parent class has to be loaded first
and that recursively for each parent class. 与构造函数执行类似,在加载类之前,必须first
加载其父类,并为每个父类递归加载。
Here classes corresponding to A
extends B
extends C
: 这里对应于A
类扩展B
扩展C
:
class C {
public C(){
System.out.println("C constructor");
}
}
class B extends C{
public B() {
System.out.println("B constructor");
}
}
class A extends B{
public A() {
System.out.println("A constructor");
}
}
public class Test {
public static void main(String[] args) {
new A();
}
}
Compile these classes and then execute the Test
class that creates a A
instance in its main()
method by specifying the -verbose
flag (enable by default the classes logs) in the java
command : 编译这些类,然后通过在java
命令中指定-verbose
标志(默认情况下启用类日志)来执行在其main()
方法中创建A
实例的Test
类:
java -verbose Test
And you should see in the output standard something like : 你应该在输出标准中看到类似的东西:
[Loaded java.lang.Object from C:\...] [Loaded java.io.Serializable from C:\...] ... // load all classes required until loaded C, B and A classes [Loaded C from file:/C:/...] [Loaded B from file:/C:/...] [Loaded A from file:/C:/...] C constructor B constructor A constructor
It confirms that : 它证实:
Object
) until the closest parent class( B
). 需要首先通过根父类( Object
)开始加载父类,直到最近的父类( B
)。 Object
) until the closest parent constructor( B
). 父类的构造函数总是在实例化类的构造函数之前执行,方法是从根父构造函数( Object
)开始,直到最接近的父构造函数( B
)。 The constructor is called, therefore an object of the parent class is created. 调用构造函数,因此创建父类的对象。 That object is used later to build an object of the child class. 稍后使用该对象来构建子类的对象。 The reason for this is that an object of the child class is an object of the parent class with more things. 这样做的原因是子类的对象是父类的对象,具有更多的东西。
Because the child class inherits properties and methods from the parent class, the entire class hierarchy (including superclasses and implemented interfaces) are loaded . 因为子类从父类继承属性和方法,所以加载了整个类层次结构(包括超类和实现的接口)。
When you create an instance of the childclass, only a single object of type childclass is created (instantiated). 当您创建childclass的一个实例,只有类型childclass的一个对象被创建(实例化)。 There are no additional instances of any parent(s). 没有任何父母的额外实例。
The properties and methods declared on parent classes are accessible via the childclass instance, and due to this inheritance the childclass instance will be accepted as argument to any methods that expect an instance of the parent (or any interfaces that it or any parent implements). 在父类上声明的属性和方法可以通过子类实例访问,并且由于这种继承,子类实例将被接受为任何期望父实例(或它或任何父实现的任何接口)的方法的参数。
If you think of the code as a contractual design , this makes perfect sense: 如果您将代码视为合同设计 ,那么这非常有意义:
extends
or implements
) it is guaranteed that the child will also automatically provide the same methods. 通过继承( extends
或implements
),可以保证子项也将自动提供相同的方法。 When you first load a class, all its ancestors are also loaded recursively. 当您第一次加载一个类时,它的所有祖先也会以递归方式加载。
When you instantiate an object you won't instantiate an additional instance of its parent. 实例化对象时,不会实例化其父对象的其他实例。 However, the object you just instantiated is itself also an instance of the parent. 但是,您刚刚实例化的对象本身也是父实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.