J2SE Fast advanced--upward transformation and its memory analysis

Source: Internet
Author: User

Remember that just learning object-oriented through C #, contact with the object transformation, recently learned Java, and encountered. In this case, we use Java to analyze the transformation of objects and use memory analysis to deepen understanding.

Upward Transformation

As we all know, dogs are one of the animals, so if there is now a dog class and animal class, then their relationship is the dog class inherits the animal class.

We often say that dogs are animals, in fact "dogs are animals" is an example of upward transformation, that is, the dog (sub-category) as an animal (the parent) to see. In a word, the upward transformation is a reference to the parent class to the instance of the subclass .

Example Description && memory Analysis

The following is a simple small example with memory analysis to understand the upward transformation.

Define parent class animal and subclass dog

Class Animal{public String name; Animal (String name) {this.name=name;} public void SetName (String name) {this.name=name;}} Class Cat extends animal{public String Furcolor; Cat (String name,string furcolor) {        super (name); this.furcolor=furcolor;} public void Setfurcolor (String color) {this.furcolor=color;}}
defining the Upcastingtest class

public class Upcastingtest {public static void main (string[] args) {Animal a=new cat ("Jingle Cat", "Blue"); A.setname ("Big Face Cat"); System.out.println (A.name);//a.setfurcolor ("yellow");//system.out.println (A.furcolor);}}
In the main method, although a variable a of type animal is defined, it points to an instance of a cat type, such as:


In the memory graph above, when the dog class is instantiated, the parent class (that is, the middle yellow part) is included in the instance of the dog in the heap memory, and the following part is only its own, while reference a in the stack memory points to the instance of the dog class in heap memory, but it is a reference to the animal type. So in the dog instance, except for the part that inherits from animal, the rest of it is not visible. So we can consider that a is essentially pointing to the part of the heap memory that belongs to the animal class (that is, the yellow part). So in the main method, if you execute the commented out code "A.setfurcolor (" Yellow ")," or "System.out.println (A.furcolor), you will get an error.

Therefore, when a reference to a parent class is directed to a subclass object, a reference to the parent class can only access the subset of properties and methods that the subclass inherits from the parent class, as well as the methods of the parent class that the child class overrides .


J2SE Fast advanced--upward transformation and its memory 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.