Why is constructor of super class invoked when we

2019-01-07 23:24发布

Consider this code:

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

    int adds(int n1, int n2) {
        return(n1+n2);
    }

    void print(int sum) {
        System.out.println("the sums are " + sum);
    }
}


class Test1 extends Test {
    Test1(int n1, int n2) {
        System.out.println("In constructor of Subclass");
        int sum = this.adds(n1,n2);
        this.print(sum);
    }

    public static void main(String[] args) {
        Test1 a=new Test1(13,12);
        Test c=new Test1(15,14);
    }
}

If we have a constructor in super class, it will be invoked by every object tht we construct for the child class(ex. Object 'a' for class Test1 calls Test1(int n1, int n2) as well as Test()).

Why does this happen?

The output of this program is:

In constructor of Superclass

In constructor of Subclass

the sums are 25

In constructor of Superclass

In constructor of Subclass

the sums are 29

18条回答
一夜七次
2楼-- · 2019-01-07 23:38

Because it will ensure that when a constructor is invoked, it can rely on all the fields in its superclass being initialised.

see 3.4.4 in here

查看更多
该账号已被封号
3楼-- · 2019-01-07 23:39

That´s how Java works. If you create a child object, the super constructor is (implicitly) called.

查看更多
该账号已被封号
4楼-- · 2019-01-07 23:41

super() is added in each class constructor automatically by compiler.

As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement.If you are creating your own constructor and you don't have either this() or super() as the first statement, compiler will provide super() as the first statement of the constructor.

enter image description here

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-07 23:43

When we create an object of subclass, it must take into consideration all the member functions and member variables defined in the superclass. A case might arise in which some member variable might be initialized in some of the superclass constructors. Hence when we create a subclass object, all the constructors in the corresponding inheritance tree are called in the top-bottom fashion.

Specifically when a variable is defined as protected it will always be accessible in the subclass irrespective of whether the subclass is in the same package or not. Now from the subclass if we call a superclass function to print the value of this protected variable(which may be initialized in the constructor of the superclass) we must get the correct initialized value.Hence all the superclass constructors are invoked.

Internally Java calls super() in each constructor. So each subclass constructor calls it's superclass constructor using super() and hence they are executed in top-bottom fashion.

Note : Functions can be overridden not the variables.

查看更多
贪生不怕死
6楼-- · 2019-01-07 23:45

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 call explicitely the super class constructor to 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
}
查看更多
不美不萌又怎样
7楼-- · 2019-01-07 23:45

"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."
(source: https://docs.oracle.com/javase/tutorial/java/IandI/super.html)

查看更多
登录 后发表回答