Upward and downward transformation, upward and downward Transformation

Source: Internet
Author: User

Upward and downward transformation, upward and downward Transformation
Downward Transformation

There are two situations in the downward transformation process:

 1 Father fh=new Father(); 2 if(fh instanceof Son) 3 { 4     Son son =(Son)fh; 5 }

 

Upward Transformation
 1 Father fh=new Son();

  

When you need to call a function of the parent class, how does the compiler know which subclass object the reference of this parent class directs?This involves dynamic binding..

  

The following is a reference to the internal implementation mechanism of JAVA Dynamic binding, Author: Apsara King Kong.

Link http://blog.csdn.net/sureyonder/article/details/5569617.

 

When a Java Virtual Machine calls a class method, it selects the called Method Based on the type referenced by the object (usually known during compilation. On the contrary, when a virtual machine calls an instance method, it selects the called method based on the actual type of the object (only available at runtime), which is dynamic binding, is a type of polymorphism. Dynamic binding provides great flexibility to solve actual business problems and is a very elegant mechanism.

  • Java object model

The JAVA virtual machine specification does not specify how JAVA objects are represented in the heap. The internal representation of the object also affects the design of the entire heap and the garbage collector, which is determined by the real-time users of the virtual machine.

The basic data contained in a JAVA object is composed of its class and its instance variables declared by all superclasses. As long as there is an object reference, the virtual machine must be able to quickly locate the data of the object instance. In addition, it must be able to access the corresponding class data (the type information stored in the Method Area) through the object reference. Therefore, an object usually has a pointer to the method area. When a program needs to convert an object to another type during running, the virtual machine must check whether the conversion is allowed, whether the converted object is indeed a referenced object or its super type. When the program performs the instanceof operation, the virtual machine also performs the same check. Therefore, the virtual machine needs to view the class data of the referenced object.

No matter what kind of object representation is used by the Virtual Machine implementation, it is very likely that each object has a method table because the method table accelerates the efficiency of calling the instance method. However, the Java Virtual Machine specification does not require the use of a method table, so not all implementations use it.

The following is a memory representation of a JAVA object:

   

 

The method data is stored in the Method Area of the class and contains the bytecode binary of the specific implementation of a method. The method pointer points directly to the starting position of this method in the memory. You can find this method through the method pointer.

 

  • Dynamic binding internal mechanism

The method table is an array pointing to the method pointer in the method area. The method table does not contain static, private, and other static binding methods. It only contains the instance methods that require dynamic binding.

In the method table, methods from superclass appear before methods from subclasses, and the order of arrangement of method pointers is the same as that of methods in class files, the exception to this sort order is that the method covered by the quilt class method appears in the superclass where the method first appears.

For example, there are super Base classes and sub-classes Derive:

  

1 public class Base 2 {3 public Base () {} 4 5 public void test () 6 {7 System. out. println ("int Base"); 8} 9 10 public void print () {} 11} 12 13 public class Derive extends Base14 {15 public Derive () {} 16 17 public void test () 18 {19 System. out. println ("int Derive"); 20} 21 22 public void sayHello () {}23 24 public static void main (String [] args) 25 {26 Base base = new Derive (); 27 base. test (); 28} 29}View Code

  

The Base and Derive methods in the preceding example are as follows:

  

In this example, the test () method is in the same position in the Base and Derive method tables: The index is 1.

When the Java virtual machine executes base. test (), the actual object memory location pointed to by the base can be found through base reference. Now the virtual machine does not know whether the actual object referenced by the base is Base or Derive. However, based on the above Object Memory Model, the VM can find the actual object type data and Class instances starting from the first pointer "special structure Pointer" in the object memory, in this way, the virtual machine can know that the actual object referenced by the base is a Derive pair. To execute test (), the VM needs to find the bytecode of test (), and the bytecode of the method is stored in the method area. The VM starts from the first pointer "special structure Pointer" in the object memory, searches for Index 1 in the method table, and the test () method pointed to by index 1 is the test () method of the Derive class, this is the bytecode of the test () to be executed by the Java Virtual Machine. Now, the virtual machine knows that the actual object to be called is the Derive object, and the actual test () method to be called is the test () method of the Derive class, therefore, the Java Virtual Machine can correctly execute the call method of the actual object referenced by the base instead of the method referenced by the base.

This is an implementation method of dynamic binding. dynamic binding can have different internal implementation mechanisms based on different Java Virtual Machine platforms and different actual constraints.

Related Article

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.