JAVA in the parent class and subclass may include all constructors

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sinat_32336967/article/details/97244049

1. subclass parent class constructor may occur

Java, the parent and child classes have no argument constructor participate in a total of 16 cases. But how many species are reasonable, we have to sort out what.
From left to right sort.
Here Insert Picture Description
The first case is correct. Because the parent class is provided while reference constructor display provides no argument constructor. So in both the constructor subclass, can be successfully invoked (display or default) no-argument constructor, not error.
The second case is correct. Subclasses parameters may be displayed with a default constructor, or calling a successful manner without reference constructor.
The third case is correct. Subclasses may be displayed without reference or default constructor manner without a successful call argument constructor.
The fourth case is correct. Compiler defaults to a subclass constructor with no arguments. With the third case.

The fifth case of error. Subclass default no-argument constructor will call the parent class constructor with no arguments, but the father did not, so an error. At this point there is a subclass of argument constructor calls the parent class is displayed if there is argument constructor, then the right. If not called, it will call a default no-argument constructor, but the parent did not like no-argument constructor, so an error. In summary will be given.
Sixth case may be an error, it may be true. There are a subclass of argument constructor calls the show if there is argument constructor of the parent class, then the right. If not called, it will call a default no-argument constructor, but the parent did not like no-argument constructor, so an error.
The seventh case error. Subclass default no-argument constructor will call the parent class constructor with no arguments, but the father did not, so an error.
Eighth case of error. Compiler defaults to a subclass constructor with no arguments. With the seventh case.

The ninth case properly. Because the parent shown to provide no argument constructor. So in both the constructor subclass, can be successfully invoked (display or default) no-argument constructor, not error.
Tenth case properly. Subclasses parameters may be displayed with a default constructor, or calling a successful manner without reference constructor.
Tenth situation correctly. Subclasses may be displayed without reference or default constructor manner without a successful call argument constructor.
Twelfth correct the situation. Compiler defaults to a subclass constructor with no arguments. Tenth same situation.

The compiler will generate a default to the parent class constructor with no arguments. So thirteenth to sixteen case with the ninth to twelve cases.

2. summary

Providing only when there is the parent class constructor parameters, but not displayed to provide no argument constructor, this most error-prone.
Should develop case provides no explicit reference constructor.

Guess you like

Origin blog.csdn.net/sinat_32336967/article/details/97244049