Super () and this () cannot coexist, or the exception will be reported at compile time.
Constructorcall must is the first statement in a constructor
In other words, super () and this () must all be in the first line of the constructor method.
This (parameter/no parameter) is used to invoke the corresponding constructor of this class
Super (parameter/parameterless) is used to invoke the corresponding constructor of the parent class
And in the constructor, the call must be written in the first row of the constructor definition and cannot be used after the constructor.
A constructor definition cannot include both the this call and the super call, and if you want to include it, you can first make a super () call in the constructor of this () call. You can also modify the Testb () method to a non-constructed method, which is called in the construction method testb (int i).
Correct explanation: Theparent class ' constructor needs to becalled before the subclass ' constructor. This would ensure if you call Anymethods on the parent class in your constructor, the parent class has Alreadybeen set Up correctly.
translation : The constructor of the previous parent class requires calling the constructor of the subclass. This will ensure that if you call any method in the parent class constructor , the parent class is already set up correctly.
2. Error: Implicit super constructor XX () is undefined for default constructor. Must define an explicit constructor
Because your parent class already has a parameter constructor defined, the compiler does not call the default constructor for you.
When a subclass inherits, the constructor of the parent class must be explicitly called in its own constructor to ensure that the child class is instantiated before the initialization of the parent class.
If you have an argument-free constructor in your parent class, the subclass will not force a call, that is, the one you wrote can pass,
The compiler will help you invoke the constructor of the parent class by default.
According to the original idea, must be into the following:
Class Person {protected string name; protected int age;//You have defined an automatic constructor, at which point the compiler does not create a default constructor for you public person (String Name,int Age) {this.name=name; this.age=age,} public void print () {System.out.println ("Name:" +name+ "/nage:" +age);}} /* Because the constructor of the parent class is a parameter, the compiler does not automatically call the default constructor for you, at which point the subclass must explicitly call the constructor of the parent class in its own constructor */class Student extends person {public Student () { //Sub-class constructor//super (); No, because your parent class does not have a parameterless constructor super ("a", 1); Displays the constructor of the calling parent class, and must be the first row of the call}} class Test {public static void main (String args[]) {}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Constructor call must is the first statement in a Constructor