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

后端 未结 18 1396
误落风尘
误落风尘 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 20:12

    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.

提交回复
热议问题