Graph Analysis of Java multi-state memory allocation

Source: Internet
Author: User
Illustration: Java multi-state memory allocation parsing person worker = new worker ();

The subclass instance object address is assigned to the parent class type reference variable. The embodiment of polymorphism.

Worker. startwork (); why can't I call it?

Because the reference variable worker is of the parent class type, it will take the reference of the person class in the parent object of the subclass, and find the type information of the class in the method area, and the startwork () method cannot be located. Therefore, the system prompts that the symbol cannot be found. [Red Arrow Track]

This is the case for upward transformation.

------------------------------

Worker W = (worker) worker;

Why can this be called?

The referenced worker variable is a reference of a subclass object.

Then, I assign the reference value to a subclass type to reference the variable, and then call the startwork () method to find the instance object based on the reference, and then the instance object passes the reference held by the instance object, locate the type information of this class in the Method Area and locate the startwork () method. OK. Method to stack, walk you ~ [Green Arrow Track]

This is also the case for downward transformation. The operation is performed on the same instance object (subclass Instance Object), but the reference is changing because the two have an inheritance relationship. And person worker = new worker (); this statement establishes the relationship between the two.

-----------------------------

Let's look at the second sentence: worker. Say (); As mentioned above, he will find the say method of the parent class. How can he execute the say method of the subclass? If you think so, you will be cheated by me, hahaha. If the say method is overwritten, the JVM will call the method of overwriting the quilt class. Don't ask me why, the Java language is so defined, and the JVM will implement it...
OriginalArticle, Reprinted please indicate the source:
Http://blog.csdn.net/thinging_in_android

 

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.