Inheritance of constructors

Constructors are a special class of methods that are the only way to create an instance of an object. Its name is exactly the same as the class name and it does not return any data type, including void. If no constructor is declared in the class, Java automatically provides a default constructor.

Constructors can be overloaded, but not overridden. When a subclass inherits the constructor of the superclass, the following principles must be followed:

(1) The subclass unconditionally inherits the parameterless constructor of the parent class and executes it automatically when a new subclass object is created.

(2) The subclass cannot inherit the constructor with parameters of the parent class, but can only call a constructor of the parent class through the super keyword.

(3) If there is no super() statement in the constructor of the subclass, when the object is created, the system will automatically call the no-argument constructor inherited from the parent class, and then execute its own constructor.

(4) In the definition of the constructor of the subclass, if you want to call the constructor with parameters of the parent class, you need to use the super keyword, and the calling statement must be the first executable statement of the constructor of the subclass.

(5) A good programming practice is that the constructor of the subclass calls a constructor of the parent class to reduce the amount of code writing.

When creating an object of a subclass, the constructor of the parent class is executed first, then the constructor of the subclass is executed, and then the constructor of the subclass is executed. The specific calling sequence of the constructor is as follows:

Step 1: If there is a parent class, call the constructor of the parent class first. If the parent class has a parent class, continue up until the top-level parent class, call its constructor, and then call it down layer by layer. Constructors of other parent classes.

Step 2: When the constructor of the parent class closest to the subclass is called, execute the constructor of the member object of the subclass.

Step 3: Finally, execute the subclass's own constructor.

package page94;
class Grandpa{
	protected Grandpa(){
		System.out.println("default Grandpa");
	}
	
	public Grandpa(String name) {
		System.out.println(name);
	}
}
class Father extends Grandpa{
	protected Father() {
		System.out.println("default Father");
	}
	
	public Father(String grandpaName,String fatherName) {
		super(grandpaName);
		System.out.println(fatherName);
	}
}
public class Son extends Father{
	public Sound() {
		System.out.println("default Son");
	}
	
	public Son(String grandpaName,String fatherName,String sonName) {
		super(grandpaName,fatherName);
		System.out.println(sonName);
	}
	
	public static void main(String[] args) {
		Son s1=new Son("My Grandpa","My Father","My Son");
		Son s2=new Son();
	}
}

The output is:

My Grandpa
My Father
My Son
default Grandpa
default Father
default Son

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450775&siteId=291194637