When a class inherits from another class, it calls the constructor. Generally, it first calls the constructor of the inherited class. If the parent class does not contain a constructor with parameters, the subclass calls the default constructor. If your parent class has a constructor with parameters, the constructor with parameters will overwrite the default constructor, your subclass must call the constructor of the parent class with parameters.
A derived class must call a base class constructor, but does not inherit from the base class constructor. In fact, it is not a nonsense because the constructor cannot be called through the class instance. Any class has constructor. If you do not write constructor, you will generate the default constructor. if you write a constructor of any form, the system will not generate Default constructors for you.
Constructor inheritance has the following restrictions:
1. Subclass unconditionally inherits the default constructor of the parent class (that is, the constructor of the intangible parameter ).
2. If the subclass defines its own constructor, when the subclass creates a new object, the execution sequence of the constructor is: first execute the non-argument constructor inherited from the parent class ---> then execute your own constructor, that is, twice;
3. If the parent class does not have a default constructor, The subclass cannot define its own constructor without parameters. For the parent class's constructor with parameters
The class can explicitly call the constructor of the parent class by using super in its own constructor. However, this call statement must be constructed as a subclass.
The first executable statement of the function.
Subclass SC = new subclass (); baseclass BC = (baseclass) SC; --- correct
Baseclass BC = new baseclass (); subclass SC = (subclass) BC; --- it is incorrect.
Baseclass BC = new subclass () is correct, and the method body executed when calling the method in BC is the method body of the subclass, but the method must be in the subclass at the same time, the parent class exists at the same time. If the child class exists but the parent class does not, BC cannot be called. submethod ();
Class base {
Public void print () {system. Out. println ("base ");}
}
Public class derived extends base {
Public void print () {system. Out. println ("derived ");}
Public static void main (string [] ARGs ){
Base P = new derived ();
P. Print (); // result is derived calls the method in the subclass.
}}
NOTE: If both classes inherit from the same class (must be directly inherited; otherwise, no value is required), the two classes can be assigned values to each other. For example, panel and frame inherit from container, so panel P = new frame (); and frame F = new Panel () are both correct.
Parent class:
Class fatherclass {
Public int I = 0;
Public fatherclass (){
System. Out. println ("fatherclass create ");
}
Public fatherclass (string text ){
I = 1;
System. Out. println ("fatherclass create ");
}}
Subclass:
Public class childclass extends fatherclass {
Public childclass (string text) {// the first line of the constructor of the subclass is to call the parent constructor. If the first line of the subclass constructor is not to call the parent constructor, the compiler automatically calls the non-argument constructor of the parent class (that is, the so-called implicit call to the parent class constructor). If the parent class does not have any constructor, the compiler reports an error. Solution: Add a non-argument constructor to the parent class.
System. Out. println ("childclass create ");
}
Public static void main (string [] ARGs ){
Fatherclass fc = new fatherclass ();
Childclass cc = new childclass ();
}}
Output result:
Fatherclass create
Fatherclass create
Childclass create