Java inheritance constructor uses process parsing

Time:2021-8-23

This article mainly introduces the use process analysis of Java inheritance constructor. It is introduced in great detail through the example code, which has a certain reference value for everyone’s study or work. Friends in need can refer to it

Initialize base class

As mentioned earlier, inheritance is the extension of a child class to a parent class《 Thinking in Java mentions the following paragraph:

When you create an object of an exported class, the object contains a child object of the base class. This sub object is the same as the object you created directly with the base class. The difference between the two is that the latter comes from the outside, while the sub objects of the base class are wrapped inside the objects of the exported class.

When we create a subclass object, we call the constructor of the parent class, or even the constructor of the parent class. We know that constructors are used to create objects, so we suddenly wonder: when creating a subclass object, will the parent object be created first?

After searching the data, it is concluded that:

did not. When creating a subclass object, the member variables and methods of the parent class will be loaded into memory. Since you want to load them, call the parent class constructor to see how these data are initialized. That’s all. It’s not the object that created the parent class.

Therefore, it can be seen that the child object contains the child object of the parent class. We know that object initialization is very important. So, how do the child objects of this parent class initialize correctly? That’s the next thing to say: in the constructor, call the base class constructor to execute initialization.
Note: subclasses cannot inherit the constructor of the parent class, but simply call the initialization code in the base class constructor.

default constructor

Let’s look at a simple test code:

package com.my.pac13;
/*Construction in inheritance*/
public class Person {
  Person(){
    System.out.println("Person()");
  }
}
class Student extends Person{
  Student(){
    System.out.println("Student()");
  }
}
class PrimaryStudent extends Student{
  PrimaryStudent(){
    //super();
    System.out.println("PrimaryStudent()");
  }
  public static void main(String[] args) {
    //The primarystudent object was created
    new PrimaryStudent();
  }
}
/*
 Person()
 Student()
 PrimaryStudent()
*/

As for constructors, we mentioned earlier that any class without an explicit constructor has a parameterless default constructor. Our example above adds printouts to the default constructor for easy understanding.

You can see:

When creating a primarystudent, the constructors in its direct parent class student and indirect parent class person are called, and you can see that they are “top-down”.
The parent class has completed the initialization operation before the child class constructor can access it.

If the subclass does not explicitly call the constructor of the parent class, the default (parameterless) constructor of the parent class is automatically called.

Constructor with parameters

In the previous code, each class contains a default constructor. When creating a subclass object, it is top-down, and the subclass will call the parameterless constructor of the parent class by default. Then, if the parent class happens to have no parameterless constructor or you want to call the parameterless constructor of the parent class, we need our super keyword( Super keyword is followed by a summary)

We directly make some modifications on the original basis and test it.

package com.my.pac13;
/*Calling a base class constructor is the first thing to do in a subclass constructor*/
public class Person {
  //There is no default constructor
  Person(String name){
    System.out.println("Person()\t"+name);
  }
}
class Student extends Person{
  //There is no default constructor, and it is explicitly called with super
  Student(String n){
  //Super keyword calls the constructor of the parent class
    super(n);
    System. Out. Println ("a parameter student \ T" + n);
  }
  Student(String n,String m){
  //This keyword calls an overloaded constructor in the same class
    this(n);
    System. Out. Println ("two parameter student() \ T" + m);
  }
}
class PrimaryStudent extends Student{
  //Implicitly calling the parent class constructor has no parameter constructor, but the parent class does not, so it should be explicitly called with super
  PrimaryStudent(){
  //No following statement will report an error
    super("hello");
    System.out.println("PrimaryStudent()");
  }

}
class ExtendsTest{
  public static void main(String[] args) {
    new Person("the shy");
    System.out.println("***********");
    new Student("rookie");
    System.out.println("***********");
    new Student("the shy","rookie");
    System.out.println("***********");
    new PrimaryStudent();
    System.out.println("***********");
  }

}
/*
Person()  the shy
***********
Person()  rookie
One parameter student rookie
***********
Person()  the shy
One parameter student the shy
Two parameter student () rookie
***********
Person()  hello
One parameter student hello
PrimaryStudent()
***********
 */
  • This is the object being created to call overloaded constructors in the same class. See my previous article: this of Java keyword.
  • Super uses a method similar to this when calling the constructor( But super is essentially different from this. Super is not a reference to an object!!!)
  • Both super and this statements must appear on the first line, that is, there can only be one of them in a constructor.

The subclass calls the parent constructor

Whether the super statement is used to call the initialization code of the parent constructor or not, the child constructor will always call the parent constructor in advance! This must be remembered!

Subclass constructor a explicitly uses super to call parent constructor B in the first line, format super (parameter list), and select the corresponding parent constructor according to the parameter list.

//Parent class
 Person(String name){
    System.out.println("Person()\t"+name);
  }
//Subclass
 Student(String n){
  //Super keyword calls the constructor of the parent class
    super(n);
    System. Out. Println ("a parameter student \ T" + n);
  }

Subclass constructor a first uses this to call constructor B overloaded by this class, and then B calls the parent constructor.

//Parent class
 Person(String name){
    System.out.println("Person()\t"+name);
  }
//Subclass
Student(String n){
  //Super keyword calls the constructor of the parent class
    super(n);
    System. Out. Println ("a parameter student \ T" + n);
  }
Student(String n,String m){
//This keyword calls an overloaded constructor in the same class
  this(n);
  System. Out. Println ("two parameter student() \ T" + m);
}

When there is no super and this in the subclass constructor, the system will implicitly call the parameterless constructor of the parent class. If there is no parameterless constructor, an error will be reported.

//Implicitly calling the parent class constructor has no parameter constructor, but the parent class does not, so it should be explicitly called with super
PrimaryStudent(){
//No following statement will report an error
  super("hello");
  System.out.println("PrimaryStudent()");
}

in summary:

When a subclass constructor is called to initialize a subclass object, the parent class constructor is always executed before the subclass constructor. Even, the parent class of the parent class will execute before the parent class… All the way back to the constructor of the superclass object class of all classes.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.