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