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-08 00:01

Parents Exits First!! And like real world Child Can't exist without the Parents.. So initialising parents(SuperClass) first is important in order to use thrm in the children(Subclass) Classes..

查看更多
Juvenile、少年°
3楼-- · 2019-01-08 00:02

Constructor implements logic that makes the object ready to work. Object may hold state in private fields, so only its class' methods can access them. So if you wish instance of your subclass be really ready to work after calling constructor (i.e. all its functionality including inherited from base class is OK) the base class's constructor must be called.

This is why the system works this way.

Automatically the default constructor of base class is called. If you want to change this you have to explicitly call constructor of base class by writing super() in the first line of your subclass' constructor.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-08 00:02

The base class constructor will be called before the derived class constructor. This makes sense because it guarantees that the base class is properly constructed when the constructor for the derived class is executed. This allows you to use some of the data from the base class during construction of the derived class.

查看更多
你好瞎i
5楼-- · 2019-01-08 00: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.
查看更多
The star\"
6楼-- · 2019-01-08 00:05

Since you are inheriting base class properties into derived class, there may be some situations where your derived class constructor requires some of the base class variables to initialize its variables. So first it has to initialize base class variables, and then derived class variables. That's why Java calls first base class constructor, and then derived class constructor.

And also it doesn't make any sens to initialize child class with out initializing parent class.

查看更多
太酷不给撩
7楼-- · 2019-01-08 00:05

As we know that member variables(fields)of a class must be initialized before creating an object because these fields represent the state of object. If these fields are explicitely not initilized then compiler implicitely provides them default values by calling no-argument default constructor. Thats why subclass constructor invokes super class no-argument default constructor or implicitely invoked by compiler .Local variables are not provided default values by compiler.

查看更多
登录 后发表回答