This and super must be placed in the first row of the constructor, because the initialization action is done first.
The first row in the constructor in the parent class also has an implicit super () statement, which points to the parent class of all classes, object.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////
Constructors in the child parent class:
When the child class object is initialized, the constructor of the parent class is also run, because the constructor of the subclass defaults to the first line with an implicit statement, super ();
Super () statement: a constructor that accesses the empty parameters of the parent class. And all constructors in the subclass default to the first row are super ();
>> Why must subclasses access constructors in the parent class?
Because the data subclasses in the parent class can be obtained directly, the subclass object needs to see how the parent class initializes the data when it is established. So when the subclass initializes the object, it accesses the constructor in the parent class first. If you want to access a constructor that is set up in a parent class, you can specify it by manually defining the super () statement. For example, the parent class has a constructor such as Fu (String name) {}, and when the subclass accesses such a constructor, it cannot use the implicit super () to access the constructor in the parent class, but must be accessed with an explicit super (string name) statement. The parent class is not implicit, and when the subclass is accessed, the Super statement cannot be implicit.
The super statement must be placed in the first row of the subclass's constructor.
Conclusion: All constructors in a subclass will default to the constructor of the parent hollow parameter. Because the first line within each constructor of a subclass has an implicit super ();
When there are no null argument constructors in the parent class, the subclass must manually specify the constructor to access by using the super or the this statement.
Of course, the first row of constructors in subclasses can also manually specify the this statement to access the constructors in this class, with at least one constructor in the subclass to access the constructors in the parent class.
Abstract class Worker{private String name; String num;double Salary; Worker (String name,string num,double salary) {this.name=name;this.num=num;this.salary=salary;} public void Show () {System.out.println ("Hello");} public abstract void work (); Public String GetName () {return this.name;} public void SetName (String parm) {this.name=parm;}} Class Emp extends Worker{emp (String name,string num,double salary) {super (name,num,salary);} public void work () {System.out.println (' Emp work ');}} Class Employee extends worker{double bonus; Employee (String name,string num,double salary,double bonus) {super (name,num,salary);//Call the constructor in the parent class using Super () ; Call the non-constructors in the parent class to use the Super. Method name. This.bonus=bonus;} public void work () {System.out.println (this.bonus+ "...") +this.num);} public static void Main (string[] args) {Employee E=new employee ("Jhon", "111", 222,333); E.setname ("Demetria");//e. Employee (); E.work (); System.out.println ("\ n" +e.getname ());}}
Java Super Statement