Question one: Can Java constructors be inherited?
The author of Java read a book said: "Java subclasses naturally inherit its superclass" non-private members.
Normally Java constructors are set to public (if you do not write the constructor, Java automatically adds the parameterless null constructor is public), because the classes in this article are in the same package, so the use of non-decorated friendly permissions to explain the problem, for the meaning of the private constructor, can be found here.
So according to the rules of the book, non-private constructors of course should also inherit the quilt class.
In practice, however, a subclass cannot inherit a private member, nor can it inherit a constructor.
This is illustrated in the code below.
Example 1.
The system automatically adds an empty constructor: (if the constructor is not written, Java adds an empty constructor without parameters by default)
[Java]View Plaincopy
- Class base{
- }
- Class derived extends base{
- public static void Main (string[] args) {
- Derived d=new derived ();
- }
- }
Example 2.
The non-parametric null constructor that the system automatically adds in the parent class is explicitly written out (in order to have the output, add println):
[Java]View Plaincopy
- Class base{
- Base () {
- System.out.println ("base constructor");
- }
- }
- Class derived extends base{
- public static void Main (string[] args) {
- Derived d=new derived ();
- }
- }
Output: base constructor, code compiled. From the example, it seems that subclasses can inherit the superclass constructor.
But the reality is: Java rules: When a subclass creates an object, it first creates an object of the parent class, so the constructor of the parent class must be called first. In Example 2, derived does not override the constructor method, according to the Java rules, the system will default to add a non-parametric construction method, and the first sentence of the method is super (). The explicit write-up is:
[Java]View Plaincopy
- Derived () {
- super ();
- }
That is why subclasses can not write, because: if the parent class "only" has no parameter construction method, and does not intend to rewrite the subclass of the construction method, in order to save the code, the subclass construction method can not write, the system calls the parent class without the parameter construction method super () by default.
Perhaps some people think that these examples are not persuasive enough, still think that the construction method can inherit, it's okay, after reading this article, especially example 4 with the last with ★ text, can fully explain the problem.
Let's take a look at the method of constructing a parameter:
Example 3.
If you change the constructor method of the superclass to a method with the constructor, the code is shown below, then eclipse will report "Implicit super constructor xxx is undefined for default constructor. Must define an explicit constructor "
Description The subclass is not inheritable for the method of constructing the superclass.
[Java]View Plaincopy
- Class base{
- Base (int x) {
- System.out.println ("base constructor,i=" +x);
- }
- }
- Class derived extends base{
- public static void Main (string[] args) {
- Derived d=new derived ();
- }
- }
For a constructor that has parameters, you must define your own constructor method in the subclass.
(Note that parameter matching, the first sentence of the super (Paramlist) to the parent class constructor method, if the multi-level inheritance, you need to pass the parameter to the topmost layer of the method of the parameter)
[Java]View Plaincopy
- Class base{
- Base (int x) {
- System.out.println ("base constructor,i=" +x);
- }
- }
- Class derived extends base{
- Derived (int x) {
- super (x); //The Super (param) is used here, and the construction method of the superclass is called.
- System.out.println ("derived constructor,i=" +x);
- }
- public static void Main (string[] args) {
- Derived d=new derived (8);
- }
- }
Output Result:
Base Constructor,i=8
Derived constructor,i=8
Question two: Can the constructor of a subclass not use super (paramlist) to call the constructor of a superclass?
The above example uses super to call the constructor of the superclass. The same book says, "the constructor of a subclass must use super to call the constructor of the superclass, and super must be the first sentence of the subclass construction method." ”
So I don't need a super, okay? The exact words are like Java syntax.
In fact, you can not explicitly write super, but the premise is "there are multiple construction methods in the superclass, and there is an explicit write-out of the non-parametric construction method."
Example 4.
A superclass has multiple construction methods, one of which is an explicit, non-parametric construction method.
[Java]View Plaincopy
- Class Base {
- Base () { //warning_su
- System.out.println ("base constructor."); //warning_su
- } //warning_su
- Base (int x) {
- System.out.println ("Base constructor,i=" + x);
- }
- }
- Class derived extends base {
- Derived () { //warning_de
- System.out.println ("derived constructor"); //warning_de
- } //warning_de
- Derived (int x) {
- //super (x); Super is commented out!
- System.out.println ("derived constructor,i=" + x);
- }
- public static void Main (string[] args) {
- Derived d= new derived (); //WARNING_CR
- Derived t = new Derived (8);
- }
- }
Output Result:
Base constructor.
Derived constructor
Base constructor. <---------note there is no i=8 here.
Derived constructor,i=8
At this point, the subclass of the constructor method that does not use super (X) to call the superclass is also compiled, but does not use Super (paramlist), new derived (8) called the superclass of the non-parametric construction method base ()!
Why emphasize the need to "explicitly write out" the parameterless construction method, you can try to comment out the code with warnning words (WARNING_SU,WARNING_DE,WARING_CR3), but also commented out super (x).
Because the superclass and subclass have a construction method (the one with the parameter), the system does not automatically add an empty constructor with no parameters to the superclass, which will cause the subclass to not find the non-parametric construction method of the superclass super () to use, also reported "implicit super constructor XXX is Undefined for default constructor. Must define an explicit constructor ".
In fact, even if we do not write super (), the system will automatically add a super (), in this case super () does not exist so the error.
Of course, subclasses do not call the superclass of the parameter constructor super (Paramlist) is relatively few cases, so the book simply must be written in the first sentence of super (paramlist).
★ In addition, if you comment out the parameterless constructor derived () in the subclass (without commenting on Warning_su and Warning_de), Eclipse will say: "The constructor derived () is undefined".
It is also proved that even non-parametric constructors cannot be inherited. In order to save effort when there is only one parameterless constructor in the superclass and no intention to rewrite the subclass constructor, the subclass constructor may not write, and Java will automatically add a super () to run when the constructor of the subclass is used.
Whether the Java constructor can be inherited, and if the subclass constructor can call the superclass constructor without using super