In this case, the execution result is 1234.
I learned that superclass constructors are always called before subclasses.
If that rule is applied, the constructor does not start from subclass ④,
The default constructor of super ();(or super (String val);) is added to the first line of the B class, and start from ① (or ②)
The execution result appears to be 11234 (or 14234).
If a subclass is specified as super ();is it out of the rule condition?
public class main {
public static void main (String [] args) {
new B ("4");
}
}
class A {
public A () {// ①
System.out.println ("1");
}
public A (String val) {// ②
this ();
System.out.println (val);
}
}
class B extends A {
public B () {// ③
super ("2");
System.out.println ("3");
}
public B (String val) {// ④
this ();
System.out.println (val)
}
}
-
Answer # 1
-
Answer # 2
Super () is implicitly supplemented only when there is no super or this.
The calling order of constructors is 4 ⇒ 3 ⇒ 2 ⇒ 1.In this case, the execution result is 1234.
This is inconsistent with the previous claim.
class Main { static void func1 () { System.out.println (1); } static void func2 () { func1 (); System.out.println (2); } static void func3 () { func2 (); System.out.println (3); } static void func4 () { func3 (); System.out.println (4); } public static void main (String [] args) { func4 (); } }
ResultsWandbox
1 2 Three 4
-
Answer # 3
Superclass constructors are always called before subclasses
This is
"The process coded in the subclass constructor can only be executed after the superclass constructor has finished"
I hope it will be understood as.
Also,
super ()
is executed implicitly even if the constructor does not explicitly call other constructors or superclass constructors at the beginning of the class. , Always the above situation.
However, if another constructor of the class is called in the constructor such asthis ()
,super ()
is executed prior to the constructor making the call. this is not allowed.The execution order of Java class constructors is such a specification.
Related articles
- order to see java constructor code
- java - method execution order
- java - i want to know about passing sql execution results between servlets
- java - error occurs in execution path specification with aop in spring
- java - about the error at the time of execution in eclipse
- java constructor definition
- about java inheritance
- java - the constructor biginteger(long) is not visible
- java - i want to store a timestamp type field in the constructor
- java - about abstract methods and inheritance
- java class inheritance
- java - can't call constructor in super
- linq - i do not know the execution order of short circuit evaluation in vbnet
- please tell me about mac os java compilation and execution
- java - i want to output characters using the constructor
- java - about method execution results
- java class inheritance relationship
- java - is it okay to work with a lower version of the execution environment?
- from java package compilation to execution
- javascript - execution order of events added by addeventlistener
I think it's generally correct. If you write down the rules more strictly:
If there is a
super (argument list);
description in the constructor implementation of the subclass, the corresponding constructor of the superclass is called at the processing timing.In cases other than the above (
super
keyword not used), callsuper ();
, that is, "super class argumentless constructor" at the top of the subclass constructor.