When creating a subclass object, the constructor of the parent class will execute first, because the first line of all the constructors in the subclass has the default implicit super(); statement
Note: the first line of the parent constructor also has an implicit super();
All classes have a "ancestor class": Object, which the parent class inherits
super(); similar to this(); in the previous article:
this(); is to call its own null parameter constructor
super(); is a null parameter constructor that calls the parent class
Example:
public class Person { public Person(){ System.out.println("Construction method of parent class"); } }
public class Student extends Person { }
public class Test { public static void main(String[] args) { new Student(); } } //Output: parent constructor
//The implicit super() is embodied here;
Focus on the memory principle of subclass and parent class:
In the object space of creating a subclass object, it is divided into two areas: one is left to the parent class, marked with super, one is left to oneself, and marked with this
The member variables of the parent class follow the child class into the area divided for the parent class
However, if the constructor of the parent class has parameters, the child class must write all
Subclasses can overload constructor, but the first line must be super()
If the parent class has multiple construction methods, the child class can call any one of them
Example:
public class Person { public Person(int a){ System.out.println(a); } public Person(double d){ System.out.println(d); } }
public class Student extends Person { public Student() { super(0); } public Student(String s) { super(1.5); System.out.println(s); } }
public class Test { public static void main(String[] args) { new Student(); new Student("..."); } } /*Output: 0 1.5 ... */
A contradiction is found here: both this() and super() must be written on the first line of the constructor, so the two keywords cannot coexist
However, it must be ensured that all construction methods of the subclass directly or indirectly call the construction methods of the parent class
Example:
public class Person { public Person(int a){ } }
public class Student extends Person { public Student(){ //Call your own constructor //Indirect form calls the constructor of the parent class this("..."); } public Student(String s){ super(2); } }
To better understand super, here is an application of super:
public class Person { private String name; private int age; public Person(String name, int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Student extends Person { public Student(String name,int age){ super(name, age); } }
public class Worker extends Person { public Worker(String name,int age){ super(name, age); } }
public class Test { public static void main(String[] args) { Worker worker = new Worker("Zhang San", 18); Student student = new Student("Li Si", 19); System.out.println(worker.getName()); System.out.println(student.getAge()); } }