1. If the base class has a default constructor, the default constructor of the base class will be called before the subclass construction:
View plaincopy to clipboardprint?
Class {
A (){
System. out. println ("A create ");
}
}
Class B extends {
B (){
// The default constructor of A will be called first.
System. out. println ("B create ");
}
}
Class C extends {
C (int I ){
// The default constructor of A will be called first.
System. out. println ("C create ");
}
}
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
Class {
A (){
System. out. println ("A create ");
}
}
Class B extends {
B (){
// The default constructor of A will be called first.
System. out. println ("B create ");
}
}
Class C extends {
C (int I ){
// The default constructor of A will be called first.
System. out. println ("C create ");
}
}
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
2. If the base class only has constructors with parameters, the subclass must explicitly call the base class constructor through super (...) in its own constructor:
View plaincopy to clipboardprint?
Class {
A (int I ){
System. out. println ("A create ");
}
}
Class B extends {
B (){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (20 );
System. out. println ("B create ");
}
}
Class C extends {
C (int I ){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (30 );
System. out. println ("C create ");
}
}
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
Class {
A (int I ){
System. out. println ("A create ");
}
}
Class B extends {
B (){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (20 );
System. out. println ("B create ");
}
}
Class C extends {
C (int I ){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (30 );
System. out. println ("C create ");
}
}
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
3. The above only describes the simplest sequence of calling constructor. In fact, the actual initialization process of an object should be:
Initializes the object's bucket to a binary value of 0.
First, recursive to the base class at the top, and use the base class at the top as the current class.
For the current class:
Call the initial setup code of the member variable in declared order.
Call the constructor.
Next, take the next inherited class as the current class. Continue step 3.
Let's take a look at the following code:
View plaincopy to clipboardprint?
Class {
A (){
System. out. println ("A create ");
}
}
Class D {
D (){
System. out. println ("D create ");
}
}
Class B extends {
Private D d D = new D ();
B (){
System. out. println ("B create ");
}
}
Class C extends B {
C (int I ){
System. out. println ("C create ");
}
}
Public class Main {
Public static void main (String [] args ){
C c = new C (10 );
}
}
Class {
A (){
System. out. println ("A create ");
}
}
Class D {
D (){
System. out. println ("D create ");