Java要求在实例化类之前,必须先实例化其超类

1.在看jvm相关书籍的时候看到子类的实例化,在子类的实例化的过程中,执行构造器时是会调父类的构造器的,那么在调父类的构造器的过程中又发生了什么呢????

2.据我理解,构造器的作用是在实例化一个对象时对其的一个初始化操作,那么子类实例化的过程中调用父类的构造器,也要将其父类实例化?????那这样的话,我岂不是有很多Object对象?因为每个类都继承object。

不太理解。百度了很多,都没有查到有提到这一点的。感觉好奇怪,难道只有我觉得很疑惑么。特来寻求答案。

资料:
clipboard.png

来源:
https://blog.csdn.net/justlov...

阅读 1.6k
1 个回答
  1. 原博文里是有一张图的 ... 会调用父类的父类的构造器,如此递归下去,直至 Object (不再有父类了)
  2. 是的,确实有很多 Object 对象 ....

======
j2se12 specification
8.8. Constructor Declarations

A constructor is used in the creation of an object that is an instance of a class

8.8.7. Constructor Body

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

8.8.9. Default Constructor

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题