Why is constructor of super class invoked when we declare the object of sub class? (Java)

后端 未结 18 1310
误落风尘
误落风尘 2020-11-27 19:30

Consider this code:

class Test {
    Test() {
        System.out.println(\"In constructor of Superclass\");
    }

    int adds(int n1, int n2) {
        ret         


        
相关标签:
18条回答
  • 2020-11-27 19:55

    Yes. A superclass must be constructed before a derived class could be constructed too, otherwise some fields that should be available in the derived class could be not initialized.

    A little note: If you have to explicitly call the super class constructor and pass it some parameters:

    baseClassConstructor(){
        super(someParams);
    }
    

    then the super constructor must be the first method call into derived constructor. For example this won't compile:

    baseClassConstructor(){
         foo(); 
         super(someParams); // compilation error
    }
    
    0 讨论(0)
  • 2020-11-27 19:57

    I'll try to answer this from a different perspective.

    Suppose Java didn't call the super constructor for you automatically. If you inherit the class, you'd have to either call the super constructor implicitly, or rewrite it yourself. This would require you to have internal knowledge of how the super class works, which is bad. It would also require to to rewrite code, which is also not good.

    I agree that calling the super constructor behind the scenes is a little unintuitive. On the other hand, I'm not sure how they could have done this in a more intuitive way.

    0 讨论(0)
  • 2020-11-27 20:00

    In simple words if super class has parameterized constructor, you need to explicitly call super(params) in the first line of your child class constructor else implicitly all super class constructors are called untill object class is reachead.

    0 讨论(0)
  • 2020-11-27 20:00

    There is a default super() call in your default constructors of sub classes.

     //Default constructor of subClass
        subClass() {
        super();
        }
    
    0 讨论(0)
  • 2020-11-27 20:01

    Constructor of Super class in called first because all the methods in the program firstly present in heap and after compilation they stores in to the stack,due to which super class constructor is called first.

    0 讨论(0)
  • 2020-11-27 20:05

    Java classes are instantiated in the following order:

    (at classload time) 0. initializers for static members and static initializer blocks, in order of declaration.

    (at each new object)

    1. create local variables for constructor arguments
    2. if constructor begins with invocation of another constructor for the class, evaluate the arguments and recurse to previous step. All steps are completed for that constructor, including further recursion of constructor calls, before continuing.
    3. if the superclass hasn't been constructed by the above, construct the the superclass (using the no-arg constructor if not specified). Like #2, go through all of these steps for the superclass, including constructing IT'S superclass, before continuing.
    4. initializers for instance variables and non-static initializer blocks, in order of declaration.
    5. rest of the constructor.
    0 讨论(0)
提交回复
热议问题