子类的对象创建是否创建了超类的对象,如果是,是否可以在子类中访问它? [英] Does object creation of subclass create object of superclass, if yes Is it possible to access it in subclass?

查看:153
本文介绍了子类的对象创建是否创建了超类的对象,如果是,是否可以在子类中访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我们实例化一个子类时,我们是否可以访问和使用间接/隐式创建的超类对象。

My question is if we can access and use indirectly/implicitly created super class objects when we instantiate a subclass .

假设 ClassA SubClassofA 的超类,我们在客户端类 ClientClass 中使用实例化 SubClassofA SubClassofA object = new SubClassofA();

Lets say ClassA is super class of SubClassofA and we instantiate SubClassofA in a client class ClientClass using SubClassofA object = new SubClassofA();

由于整个继承层次结构在我们创建子类对象时被实例化,我想知道,是否可以访问类<$ c $的对象c>客户类中的ClassA ?

Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA in client class?

如果不可能,可能是什么原因?如果我们可以访问超类对象而不重新创建它们,它会不会节省大量的堆内存?

If not possible, what might be reasons? Wouldn't it save lots of heap memory if we can access super class objects without recreating those?

我可能误解了构造函数链接和继承层次结构的整个概念但是请让它我知道你对此的看法。

I might have misunderstood whole concept of constructor chaining and inheritance hierarchy but please let me know your thoughts on this.

public class ClassA {}

public class SubClassofA extends ClassA {}

public class ClientClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SubClassofA object = new SubClassofA();

        //Above construct means that an instance of super class ClassA exists too 
        // If we can use those super class instances directly, will it result in memeory saving?
        //is it even possible to access implicitly created super class objects tied to subclass?


    }
}


推荐答案


由于整个继承层次结构在我们创建子类对象时被实例化,我想知道,是否可以在客户端类中访问类ClassA的对象?

Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA in client class?

这是很多人感到困惑的事情。如果你创建子类的对象, 意味着它创建超类的对象。

This is something lot of people get confused. If you create object of subclass, that does not mean it create object of super class.

它只是一个调用超类的构造函数,只是为了确保在超类 中初始化所有必需的字段,但这不会创建超类的对象

Its just a call to constructor of super class, just to ensure that all the required fields are initialized in super class, but this does not create object of super class.

这个问题将帮助您理解这个概念。

This question will help you, understanding the concept.


检查Kevin的回答:

Check Kevin's answer:

它不会创建两个对象,只有一个: B

It doesn't create two objects, only one: B.

从其他类继承时,你必须在你的构造函数中调用super()。如果不这样,编译器将为您插入该调用,您可以清楚地看到。

When inheriting from another class, you must call super() in your constructor. If you don't, the compiler will insert that call for you as you can plainly see.

调用超类构造函数,因为否则对象将保留为未初始化状态,可能是子类开发人员不知道的。

The superclass constructors are called because otherwise the object would be left in an uninitialized state, possibly unbeknownst to the developer of the subclass.

这篇关于子类的对象创建是否创建了超类的对象,如果是,是否可以在子类中访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆