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条回答
Fickle 薄情
2楼-- · 2019-01-07 23:51

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.

查看更多
Juvenile、少年°
3楼-- · 2019-01-07 23:51

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

 //Default constructor of subClass
    subClass() {
    super();
    }
查看更多
趁早两清
4楼-- · 2019-01-07 23:52

here your extending Test to your test1 class meaning u can access all the methods and variable of test in your test1. keep in note that u can access a class methods or variable only if memory is allocated to it and for that it need some constructor either a default or parameterized ,so here wen the compiler finds that it is extending a class it will try to find the super class constructor so that u can access all its methods.

查看更多
We Are One
5楼-- · 2019-01-07 23:53

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.

查看更多
做个烂人
6楼-- · 2019-01-07 23:54

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.

查看更多
看我几分像从前
7楼-- · 2019-01-08 00:01

The subclass inherits fields from it's superclass(es) and those fields have to get constructed/initialised (that's the usual purpose of a constructor: init the class members so that the instance works as required. We know that some people but a lot more functionality in those poor constructors...)

查看更多
登录 后发表回答