The whole process of Java inheritance and initialization analysis

Source: Internet
Author: User

Introduction to Java initialization

in general, in Java, the compiled code generated by each class exists in its own standalone file, which is loaded only when the program code is used, that is, the code of the class is loaded only when it is first used. However, if there is static, it is different, and loading occurs when the static field or static method is accessed. The most common static method is the constructor method, although the static keyword is not shown. So, to be more precise, the Java class should be loaded when any of its static members are accessed.

The concept of Java inheritance

the three main features of Java object-oriented are inheritance, encapsulation, polymorphism. The concept of inheritance, like a parent-child relationship, child inheritance father. inheritance is often thought of as a is-a relationship, that is, the subclass is a parent tired and can be understood this way. For example, a rhubarb duck is a duck. While inheritance is a significant feature of object-oriented, inheritance should be used with caution in terms of the schema of the project, with a combination of common patterns, which are objects that hold another class in one class, such as when we want to construct a car, we always hold the concrete objects of these classes, such as engines, doors, windows, wheels, etc. Obviously the car is not an engine, nor is it a wheel, their relationship is has-a, so they need to be designed using a combination pattern. One advantage of using inheritance in OOP is that it can be transformed upward (upcasting), that a subclass can hold a method of a parent class, or that a subclass is a parent class, which, through the upward transformation, leads to the advantage of polymorphism, that is, when a method is overwritten, the subclass object of the parent class type can be looked up from the subclass, Knowing that the method can be called, this advantage lays the foundation for the extensibility of the program, such as to create a new subclass, only to re-overwrite a method already in the parent class, you can implement the polymorphic characteristics when called. However, the use of inheritance should be as cautious as possible, before use should ask yourself whether you need to change upward, if the subsequent business, must be in the upward transformation, then the use of inheritance; otherwise, large can not use inheritance.

So, in inheriting this design pattern, how does the program's loading process proceed? Will the construction method of the parent class load? If there is a static field or method, how does it load?

contains the inherited program loading process

below, we explore the issue with two classes of an inheritance relationship, which contains a static field.

First, write the code for the base class:


Package Demo0812.demo1;public class Supclass {private int i=9;protected int j;public supclass () {System.out.println ("i=" +i+ ", j=" +j); j=39;} private static int X1=printinit ("supclass.x1 initialized");p rotected static int printinit (String str) { System.out.println (str); return 47;}}
Write out the code for the subclass:



Package Demo0812.demo1;public class Subclass extends Supclass {private int k=printinit ("subclass.k initialized");p ublic Subclass () {System.out.println ("k=" +k); System.out.println ("j=" +j); private static int X2=printinit ("subclass.x2 initialized");p ublic static void Main (string[] args) {System.out.println (" Subclass Constructor "); Subclass Subclass = new Subclass ();}}
As you can see from the two class codes above, the base class contains a static method and a static field, plus a display construction method. In the process of execution, of course, first access to the Subclass.main () method, which is the entry of the program, So the loader starts to start and finds out subclass's compiled code (subclass.class file), and then, noticing that the class has a keyword extends that it has a parent class, the loader loads the code of the parent class, the Supclass.class file. The loader discovers that the parent class contains a static field, so the loader loads the static field and loads the static method, so the output
SUPCLASS.X1 initialized
, and also assigns the value of X1 to 47. After loading the static area of the parent class, loading the static area of the subclass, the loader sees that there is also a static field X2 in the subclass, and initializes it, so the output
SUBCLASS.X2 initialized
, and at the same time, assign the value of X2 to 47. At this point, the static extents of the parent class and subclass are loaded, and then the main function is read, first the first line in main is output
Subclass Constructor
Next, the second line of the command, the child class instantiation, notice that the subclass is instantiated before the parent class will be instantiated, and the output
I=9,j=0
Next, the sub-class is executed sequentially, first outputting
SUBCLASS.K initialized

and the value of K is 47, the last output


k=47j=39



This is the overall loading process for this code.





The whole process of Java inheritance and initialization analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.