Java super() inheritance

前端 未结 2 908
小蘑菇
小蘑菇 2021-01-22 00:17

A short summary of the question: I have a parent class which is extended by a child class.

     public class Parent{

        public Parent(){
          //constr         


        
相关标签:
2条回答
  • 2021-01-22 00:52

    super() is calling constructor one level up on the inheritance and it is called implicitly, if there is a no-arg constructor.

    Objects are initialized from the top level of inheritance, in your case it is Object > Parent > Child > Grandchild.

    From the documentation:

    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. 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. 
    
    0 讨论(0)
  • 2021-01-22 00:59

    That would call the Child class constructor, which in turn will call the Parent class constructor.

    0 讨论(0)
提交回复
热议问题