In Java, during the construction process of a subclass, the constructor of its parent class must be called. This is because when an inheritance relationship exists, the subclass must inherit the contents of the parent class. By what means?
The answer is as follows:
When you new a subclass object, you must first create a new object of the parent class. This parent class object is located inside the subclass object. Therefore, the subclass object is larger than the parent class object. The subclass object contains This is the real situation in memory. When the construction method is new an object, it must be called. This is a rule. If you want to new the parent class object, you must call its construction method, so :
The first rule: During the construction process of a subclass, the construction method of its parent class must be called. For a class, if we do not write a constructor, the compiler will help us add a default constructor. The so-called default constructor is a constructor without parameters, but if you write the constructor yourself, then the compiler will It will not be added for you, so sometimes when you create a new subclass object, the constructor of the subclass must be called, but in the constructor of the subclass, we do not explicitly call the constructor of the base class, that is, there is no Writing, such as: super(); is not written like this, but it will call the constructor without parameters of the parent class. If there is no constructor without parameters in the parent class, an error will occur.
The second rule: If there is no explicit call to the base class constructor in the constructor of the subclass, the system defaults to calling the parameterless constructor of the base class. Note: If there is no explicit call to the base class constructor in the constructor of the subclass. , and there is no default no-parameter constructor in the base class, a compilation error occurs. Therefore, usually we need to display: super (parameter list) to call the parent class's constructor with parameters.
//If you define a new constructor
public Animal(String name) {
this.name = name;
}
}
public Dog extends Animal{
//At this time, you have to explicitly call the constructor of the parent class, because the subclass calls the parent class by default.
//No parameter constructor Animal()
public Dog(){
super("puppy"); //Display the parameterized constructor method of calling the parent class
.... //Constructor method processing of subclasses
}
}
//Of course, if you explicitly write the parameterless constructor in the parent class, for example:
class Animal{
private String name;
//Construction method without parameters
public Animal() {
..... //deal with
}
/*
If you define a new constructor, then in the constructor of the subclass, you can call the constructor of the parent class without explicitly calling it, because the subclass has a parameterless constructor.
The subclass will automatically call the parameterless constructor defined by the parent class in the constructor.
*/
public Animal(String name) {
this.name = name;
}
}