[英]Does an instance of superclass get created when we instantiate an object?
Does an instance of superclass get created when we instantiate a particular class in java. 当我们在java中实例化一个特定的类时,是否会创建一个超类实例。 If that is the case then there would be a lot of overhead of instantiating all the super classes. 如果是这种情况,则会有很多实例化所有超类的开销。 I tried following code: 我试过以下代码:
public class AClass {
public AClass() {
System.out.println("Constructor A");
}
}
public class BClass extends AClass{
public BClass(){
System.out.println("Constructor B");
}
}
public class Test {
public static void main(String[] args) {
BClass b = new BClass();
}
}
The output of the code is : 代码的输出是:
Constructor A
Constructor B
So, does it mean that the complete hierarchy of the objects of the superclasses get created when we instantiate a class? 那么,这是否意味着当我们实例化一个类时,会创建超类对象的完整层次结构?
A single object is created - but that object is an instance of both the superclass and the subclass (and java.lang.Object
itself). 创建单个对象 - 但该对象是超类和子类(以及java.lang.Object
本身)的实例。 There aren't three separate objects. 没有三个单独的对象。 There's one object with one set of fields (basically the union of all the fields declared up and down the hierarchy) and one object header. 有一个对象有一组字段(基本上是在层次结构中上下声明的所有字段的并集)和一个对象标题。
The constructors are executed all the way up the inheritance hierarchy - but the this
reference will be the same for all of those constructors; 构造函数在继承层次结构中一直执行 - 但是对于所有这些构造函数, this
引用将是相同的; they're all contributing to the initialization of the single object. 它们都有助于单个对象的初始化。
Yes. 是。
BClass constructor looks like this: BClass构造函数如下所示:
public BClass ()
{
super (); // hidden line, added by compiler
System.out.println("Constructor B");
}
If you do not want to use default constructor, you can do like this: 如果您不想使用默认构造函数,可以这样做:
public BClass ()
{
super (parameters); // now you will use different constructor from AClass
// compiler will not add here call to "super ()"
System.out.println("Constructor B");
}
From oracle site: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. 从oracle站点:如果构造函数没有显式调用超类构造函数,Java编译器会自动插入对超类的无参数构造函数的调用。 If the super class does not have a no-argument constructor, you will get a compile-time error. 如果超类没有无参数构造函数,则会出现编译时错误。 Object does have such a constructor, so if Object is the only superclass, there is no problem. 对象确实有这样的构造函数,因此如果Object是唯一的超类,则没有问题。
Yes, that's the whole point of class inheritance. 是的,这是类继承的全部要点。
You're not instantiating two objects, though: you're instantiating one object, and running both the AClass
and then the BClass
constructor on it. 但是,您没有实例化两个对象:您正在实例化一个对象,并在其上运行AClass
和BClass
构造函数。 The AClass
constructor is responsible for initializing the parts that were inherited from AClass
, and the BClass
constructor is responsible for initializing the additional things defined in BClass
. AClass
构造函数负责初始化从AClass
继承的部分,而BClass
构造函数负责初始化BClass
定义的其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.