简体   繁体   English

在实例化对象时是否创建了超类的实例?

[英]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. 但是,您没有实例化两个对象:您正在实例化一个对象,并在其上运行AClassBClass构造函数。 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.

相关问题 创建子类实例时是否还会创建超类实例? - Does a superclass instance also being created when creating a subclass instance? 创建派生类的对象时是否创建了超类的对象? - Does the object of superclass created when object of derived class is created? 当我们使用new运算符在java中的字符串中实例化时,在哪里创建了第三个对象 - When we instantiate in string in java using new operator where is 3rd object created 在继承中,当为子类创建对象时,是否还为其超类创建了一个对象? - In inheritance, when an object is created for subclass, is an object also created for its superclass? 获取对象类并实例化新实例 - get class of object and instantiate new instance 在Java中,在超类中创建的某些类的对象是否可以在子类中使用 - In Java, Does Object of some class created in superclass can be used in subclass 创建新的子类对象时是否存在超类实例? - Do superclass instances exist when a new subclass object is created? 当我们使用超类引用变量创建子类的对象时,内存如何分配给超类和子类成员 - How memory allocates to superclass and subclass members when we create object of subclass using superclass reference variable 将子类对象分配给Superclass实例类型仅用于覆盖概念?或者我们也在为其他东西执行此操作? - Assigning subclass object to Superclass instance type is only for overriding concept?or we are doing this for something else also? 如何在Java中获取SuperClass的实例? - How to get an instance of SuperClass in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM