Java subclasses inherit the parent class the program run order of in depth analysis

  • 2020-04-01 02:00:14
  • OfStack

We often use inheritance in projects, but we often don't understand the order and principle of running the program, especially when we use the transition object, and when the parent class has static variables and methods, we don't know who to run first. I also wrote an example. So that's a summary.

 The parent class: 
public class TestStatic {

    public static String name="china";

    {
       System.out.println("======== Method body ========");
    }

    static{
       name="England";
       System.out.println("======== Static block ======");
    }

    TestStatic(){
       System.out.println("========= A constructor ========");
    }

    public static void main(String[] args){
       System.out.println("======== The main method ========"+name);
    }

    public void test(){
       System.out.println("======== The test method =========");
    }
}

 The subclass: 
public class TestExtendStatic extends TestStatic{

    //public static String name="HUBEI";

    {
       System.out.println("======== Nameless method body ========");
    }

    static{
       //name="SUIZHOU";
       System.out.println("======== Subclass static block ======");
    }

    TestExtendStatic(){
       System.out.println("========= Subclass constructor ========");
    }

    public void test(){
       System.out.println("======== Subclass test method =========");
    }

    public static void main(String[] args){
       System.out.println("======== Subclass master method ========"+name);
       TestStatic ts = new TestExtendStatic();//Upper transformation object
       ts.test(); 
    }
}
 The output is as follows: 
======== Static block ======    : the parent class static block 
======== Subclass static block ======   : a subclass static block   [not a static method] 
======== Subclass master method ========England   : subclass the main method 
======== Method body ========  : non-static block of code in parent class 
========= A constructor ========  : parent class constructor 
======== Nameless method body ========  : subclass non-static code block 
========= Subclass constructor ========  : subclass constructor 
======== Subclass test method =========  : subclass test method 

Execution order: The parent class static variables and static blocks -- the child class static variables and static blocks -- the parent class non-static code blocks -- the parent class non-static code blocks -- the parent class non-static code blocks -- the child class non-static code blocks -- the constructor in the child class -- the next thing is where the object is called
Method.
As long as the object is created with new and memory space is allocated, the above method must be executed either by assigning a reference to the upper transition object or to a subclass object.
That is: TestStatic ts = new TestExtendStatic(); // upper transformation object
      TestExtendStatic ts = new TestExtendStatic(); // subclass object
The bold program above will execute.

Ts. Test (); Ts as the above transition object calls the method in the parent class that the subclass inherits, and since test () is overridden in the subclass, the output is the statement in the subclass.

If the main method in the subclass should be as follows:

public static void main(String[] args){
       System.out.println("======== Subclass master method ========"+name);
       TestStatic  ts = new TestExtendStatic();
       ts.test();
       System.out.println("-------------------------");
       ts = new TestExtendStatic();
       ts.test();
    }
 Output: 
======== Static block ======   Static block in parent class 
======== Subclass static block ======  A static block in a subclass 
======== Subclass master method ========England  The main method in a subclass 
======== Method body ========   The parent class is a non-static block of code 
========= A constructor ========  Constructor in the parent class 
======== Nameless method body ========  Subclasses are not static blocks 
========= Subclass constructor ========  Constructor in a subclass 
======== Subclass test method =========  The method that the object specifically invokes 
-------------------------  Static variables and blocks are executed only once 
======== Method body ========  The parent class is a non-static block of code 
========= A constructor ========  Constructor in the parent class 
======== Nameless method body ========  Subclasses are not static code blocks 
========= Subclass constructor ========  Constructor in a subclass 
======== Subclass test method =========

If you change the subclass main method to:

TestStatic  ts = new TestStatic ();//Using the parent class constructor
ts.test();
 The output is: 
======== Static block ======  Parent static block 
======== Subclass static block ======  Subclass static block   Because the program runs in a subclass, the subclass's static block must run. 
======== Method body ========   The parent class is a non-static block 
========= A constructor ========  Parent class constructor 
======== The test method =========  Superclass concrete method test()
 If you put the above code in the parent class, the subclass will not be loaded   Static block. 

 Through the above   We can also see that the static block runs   Before the main method, the non-static block runs after the main method. 
 I'm in the parent class   Creates an object in the main method   call test (), the result of operation: 
======== Static block ======  Static code block 
===main==
======== Method body ========  Non-static code blocks 
========= A constructor ========  A constructor 
======== The test method =========

Conclusion:
When the program runs (in a class), the static blocks of running code are loaded the first time, and once the object is created, the non-static blocks of code and the no-argument constructor are executed. In inheritance, the program will load the static block in the parent class first and then load the static block itself. Once the object is created (using the subclass constructor), it will call the parent non-static block, the parent constructor, and then the non-static block itself, the parent constructor.

Related articles: