The relationship between constructors during Java inheritance. Let's take a test to show you:
In inheritance, which of the following statements about constructor is false? () A. Subclass unconditionally inherits the non-argument constructor of the parent class, b. Subclass can reference the constructor with parameters in the parent class and use the super keyword. C. If the constructor does not have a constructor, the parent class has no constructor as its own constructor,
D. If the child class has no constructor, and the parent class has no constructor, the constructor will be overwritten.
Let's take a look at two examples:
1 public class parent {
2 public static void main (string ARGs []) {
3
4}
5 Public parent (){
6 system. Out. println ("parent class ");
7}
8}
1 public class testextends extends parent {
2 public static void main (string ARGs []) {
3 testextends tes = new testextends ();
4 tes. testmode ();
5}
6 Public testextends (){
7 system. Out. println ("testextends class ");
8}
9
10 public void testmode (){
11 system. Out. Print (100% 3 );
12 system. Out. Print (",");
13 system. Out. println (100% 3.5 );
14}
15}
Output result:
Parent class
Testextends class1. 2.0
Conclusion It is obvious that the non-parameter constructor of the subclass will not overwrite the parent class, And will first call the constructor of the parent class before calling its own.