Explain in detail the loading order of inheritance relationship classes in Java

Explain in detail the loading order of inheritance relationship classes in Java

Example code:

Operation results:

According to the running results, it is clear at a glance that before executing new sonclass() in the main method, the static code block in the class is executed after the class is loaded. Then enter the main method and execute the new operation. Of course, it is obvious that when executing the new subclass operation, the construction of its parent class must be carried out first, that is, the construction code block of the parent class (the code wrapped only in braces in the code) and the constructor must be executed first, and then the construction code block and constructor of the subclass must be executed.

Modify the code and see the results of the operation:

The order of operation is:

The first rule: during the construction of a subclass, the construction method of its parent class must be called. A class, if we don't write constructors, Then the compiler will help us add a default constructor (that is, the constructor without parameters). However, if you write the constructor yourself, the compiler will not add it to you. Therefore, sometimes when you create a new subclass object, you must call the constructor of the subclass, but if we do not display the constructor of the base class in the subclass constructor, such as super (); This will call the constructor without parameters of the parent class.

The second rule: if the constructor of the subclass does not display a method to call the constructor of the base class, and there is no parameterless constructor in the base class, there will be a compilation error. Therefore, we usually need to display: Super (parameter list) to call the constructor of the parent class with parameters. At this time, the parameterless constructor will not be called.

In short, one sentence: the subclass does not show that it calls the parent constructor. No matter whether the subclass constructor takes parameters or not, it calls the parent nonparametric constructor by default. If the parent class does not, there will be a compilation error.

Or two classes, let's change it again.

After looking at the above two examples, the last example is easily confused. Some people may think that the running result is similar to this:

Or:

But the real result is this:

Why? In fact, it's easy to understand as long as you open the comment in the parent constructor in the code: system out. Println (this. Getclass()) results in:

Yes, the this reference in the parent class is the instance object of the child class, so the printname() method of the child class is called in the constructor of the parent class. I'm quite sure. My humble opinion is that although we call the constructor of the parent class, we do not instantiate the instance object of the parent class, so this still points to the reference of the child class.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>