java-Preliminary Understanding-nineth-inheritance-constructors in child-parent classes-instantiation of subclasses-memory plots

Source: Internet
Author: User

One.

Now based on the inheritance, wrote a program, the object in memory to create the process, the initialization of the steps to show you.

The concrete process is demonstrated by specific examples and printing methods.

For the parent class, we don't care much about it, and we can write it implicitly here.

There is a show () method in the subclass, there is also a show () in the parent class, and the two are identical, which is overwritten. The subclass also has its own super (), and return.

If we create an object directly in the creation object, does the DOS run with the result? As a result, in the creation of the object, during initialization, there are output statements inside the constructor.

Watch the above, just create objects, DOS results for Zi show .... 0. There is output, but I thought I wouldn't call the show () to the subclass, or the knowledge of design coverage, I don't understand enough ?

After the code has been modified (the Z object is added to call the Show method), the output is repeated, and the DOS is displayed as two lines.

For the present result, this is what we have to say about the few details that are involved in the process of instantiating a class.

The last class, class ExtendsDemo5 classes do not draw, a main method, a constructor.

Our focus is on the changes in the sub-parent class.

First the main function is in the stack, and the reference variable z is loaded. The child class object is created, and the subclass inherits the parent class, and the parent class inherits the root class, object. When the parent loads, the object is not loaded first, it already exists. When the JVM starts, it loads a lot of things, all objects, no root class object, and these objects do not load at all. Therefore, you do not have to control the object class, as long as the parent class is OK.

Inheriting the parent class based on the subclass, the parent class is advanced memory. Inside the table that stores the parent's method. ( as you can see, the DOS result should also have a show method that outputs the parent class .) )

After the parent class is loaded, the child class is loaded immediately, and the subclass has super (), which points to the parent class. Subclass of Space, also has its own content, so that the subclass is loaded complete.

This is the time to start the new Zi object, to load many classes into the method area. You have to have classes before you can create objects. At this time, in the heap there is a subclass object, assigning an address, and there is a member variable num. This num, initialized is 0, which we call the default initialization. The result is initialized to 8, but the DOS results are displayed as 0. This involves a question of when the default initialization and reality initialization is done?

Now let's delve into the sub-class constructor, add an output statement, and then compile and run, and the DOS result is as follows.

System.out.println ("Zi cons Run") after Super (), and the null argument constructor in the parent class called the Show () method, which means that the parent class was initialized with a value of num? This num has no value, only the default value. Tell everyone how to initialize it. The default initialization for NUM is done by default. This new Zi object calls the corresponding constructor (i.e. the Zi () method, the sub-class Hollow parameter default function), this time, the constructor is in the stack, then, 8 has not yet.

The child's constructor is in the stack, and has its own, this sub-structure after the stack, there is a super (). This super means flying to the parent class, which immediately follows the stack, because there is super.

when the parent class is in the stack, it will also have the same this,this value as the subclass, because everyone defaults to the same object (this block is not explained, because it is somewhat related to polymorphism). Once the parent is loaded, the super inside is executed, referring to the object, which does not need to be taken care of. The following parent class starts calling the parent class show (), which would have been the case. When running, an overlay occurs when the same function appears in the child's parent class. When the parent class is called, if there is this, the current object is 0x0034.

What object, subclass object, is the current object. When the subclass object is running the show () method, it first looked in its own area, and it first found its own show (). It prints the "Zi show ..." +num (0). Look at num in memory below, which is 0. At the end of this operation, the parent class constructor is out of the stack.

After the stack, the equivalent of super () in the subclass ended, but then it was System.out.println ("Zi cons run ..." +num) with Num in it. The following is a new program. There is no output statement in the subclass. It is initialized to the parent class before the initialization of the parent class is completed before the turn to the display initialization of the subclass (all the parts are started after the subclass constructor is loaded, the load goes to the parent class to initialize, the initialization ends, and then goes back to the subclass constructor).

The term is the default initialization, display initialization, the last constructor initialization, at that time there is no parent class, no way to speak super problem. In fact, there is a watershed in the constructor, it is necessary to execute the subclass constructor first, but to execute super () in the subclass, the parent class is finished. The subclass initialization is performed before executing the code inside the super (). However, if there is no parent class, to do that demonstration results, often is the display initialization, subclasses in the access process has values, as if the display initialization in the constructor, the constructor is actually loaded first, the parent is busy, and then busy their own, this is necessary. We start by initializing the parent class and then working on our own, when Num becomes 8. When you execute the OUTPUT statement in the subclass, Num becomes 8.

Just behind Super () is a watershed. When the object space is opened, the member variable must be initialized by default after the address number has been divided, and there has to be a value. The default initialization is always the first step.

Then a return in the subclass constructor, which is also the stack.

At this point, the object is initialized. This is the time to assign 0x0034 to Z,z to point to the 0x0034 object.

Next, when you run the Z.show () statement, or find the Show method in the subclass, Num is 8.

Creating objects is not necessarily in the stack, see.

Look at the figure below, before num=10, is from 0-8-10, is such a process.

java-Preliminary Understanding-nineth-inheritance-constructors in child-parent classes-instantiation of subclasses-memory plots

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.