In Java, the constructor of its parent class must be called during the construction of the subclass, because when there is an inheritance relationship, the subclass inherits the contents of the parent class, and by what means does it do so?
The answers are as follows:
When you new a subclass object, you must first want to new a parent class of the image, the parent object is located inside the subclass object, so that the subclass object is larger than the parent object, the child class object contains a parent class object, This is the real case in memory. The constructor method is a method that must be tuned when you are new to an object, which is the rule that the new parent class object is to be called, so it is necessary to call its constructor method, so:
The first rule: during the construction of a subclass, you must call the constructor of its parent class. A class, if we do not write the construction method, then the compiler will help us to add a default construction method, the so-called default constructor method, is no parameter construction method, but if you write your own method of construction, then the compiler will not add to you, so sometimes when you new a sub-class object, The construction method of the subclass is definitely called, but we do not show the constructor method of calling base class in the subclass construction method, that is, no write, such as super (); This is not the case, but this invokes the constructor of the parent class without arguments, and if there are no constructor methods with no arguments in the parent class, an error occurs.
The second rule: if you call the base class construction method that is not displayed in the constructor of the subclass, the system defaults to calling the base class parameterless construction method Note: If the constructor method of the subclass does not show a call to the base class construction method, and the base class does not have a default parameterless constructor, the compilation error occurs, so Usually we need to display the super (parameter list) to call the parent class with arguments to the constructor.
Copy the Code code as follows:
//When you are not using the parent class default constructor method, you need to display the constructor method defined by the calling parent class in the constructor method of the child class.
class animal{
private String name;
//If you define a new construction method
Public Animal (String name) {
this.name = name;
}
}
Public Dog extends animal{
/////You should then display the constructor of the calling parent class, because the child class is called by default to the parent class 's
//Non-parametric construction method animal ()
Public Dog () {
super ("Puppy"); Shows the method of calling the parent class with a parameter constructor
.... //Sub-class construction method handling
}
}
//Of course, if you put the parameterless constructor in the parent class, the display is written out, for example:
class animal{
private String name;
//Non-parametric construction method
Public Animal () {
.../ /handling
}
/*
If you define a new constructor, then in the constructor of the subclass, you can call the constructor of the parent class without displaying it, because the subclass has an argument-free construction method,
subclasses automatically invoke the parameterless construction method that the parent class has already defined in the constructor method.
*/
Public Animal (String name) {
this.name = name;
}
}
Summary: However, in general, the overload of the constructor method is used in the parent class, and the corresponding parent class construction method can be called as needed in the subclass.
Class Point {//define ' points ' classes
//x axis coordinates and y-axis coordinates, which are modified to protected because they are intended for inheritance
protected float MX, MY;
Public point (float x, float y) {//constructor method
MX = x;
MY = y;
}
}
Class Circle extends Point {//define "Circle" class inherits from "Dot" class
protected float Mradius; Radius
Public Circle (float R) {//constructor method
Mradius = r;
}
}
Public class Demo {
Public static void Main (string[] args) {
Circle C = New Circle (2.5f); Instantiate a "Circle" class object
}
}
why this paragraph of the procedure error
In the memory mechanism, the parent and child classes occupy the same piece of memory, except that the subclasses add their own parts (attributes) on the basis of the parent class. Subclasses are dependent on the parent class, first the parent class and then the child class.
The generation of a subclass object, you must first call the parent class's constructor to produce a parent class instance, and then add your own part on top of the instance.
The execution order of the derived class construction method "
Use this () to invoke other constructor methods of this class
To invoke the constructor of a parent class using the super () statement
If the subclass's constructor does not use super to call the parent class construction method, nor does it call the overloaded constructor method with the This keyword, the system defaults to calling the parent class parameterless constructor, which is equivalent to Super (), and if there is no parameterless constructor in the parent class, the compilation error
Super () or this () must be in the first line of the construction method, only one sentence.
The Circle class inherits the Point Class! In the constructor of a subclass, the constructor of the parent class must be called (explicitly or implicitly), but the constructor of the point class is not called in the constructor of the circle class in your code!
If a parameterless constructor exists in the parent class, the constructor of the subclass is called implicitly, but if there is no parameterless constructor in the parent class, the constructor of the child class needs to use super to invoke the constructor of the parent class, for example:
Public Circle (float r) {
Super (0, 0);
......
}
==========
A subclass can use Super to invoke the parent class constructor, call the parent class method, and access the parent class property. Of course, the constructors, methods, and properties of the parent class must be visible to the child class, which means that the access level cannot be private, and so on!
Go Analysis of the problem of calling parent class construction method in Java Neutron class