Deep understanding of JAVA polymorphism principles and deep understanding of java Polymorphism
I have always known what polymorphism is, and I often use polymorphism when coding, but I have never really understood what the underlying operating mechanism of polymorphism is like, write it down specially, and make progress together with all of you. I also hope you can guide and correct me.
The concept of polymorphism: a single operation acts on different objects and can have different interpretations and execution results. This is polymorphism. Simply put, the reference of the parent class points to the subclass object. Next, let's take a look at the code.
1 package polymorphism; 2 3 class Dance {4 public void play () {5 System. out. println ("Dance. play "); 6} 7 public void play (int I) {8 System. out. println ("Dance. play "+ I); 9} 10} 11 12 class Latin extends Dance {13 public void play () {14 System. out. println ("Latin. play "); 15} 16 public void play (char c) {17 System. out. println ("Latin. play "+ c); 18} 19} 20 class Jazz extends Dance {21 public void play () {22 System. out. println ("Jazz. play "); 23} 24 public void play (double d) {25 System. out. println ("Jazz. play "+ d); 26} 27} 28 public class Test {29 public void perform (Dance dance) {30 dance. play (); 31} 32 public static void main (String [] args) {33 new Test (). perform (new Latin (); // Upcasting34} 35}View Code
Execution result: Latin. play. At this time, you may find that the perform () method does not have a judgment similar to "if parameter type = Dance/Latin". In fact, this is the characteristic of polymorphism, it eliminates the coupling relationship between types, so that we can treat an object as its specific type rather than its base class. Because the above Test code can be written like this:
1 public class Test {2 public void perform (Latin dance) {3 dance. play (); 4} 5 public void perform (Jazz dance) {6 dance. play (); 7} 8 public static void main (String [] args) {9 new Test (). perform (new Latin (); // Upcasting10} 11}View Code
However, you will find that if you add more new classes similar to perform () or export from Dance, a lot of work will be added, through comparison, we will know that the first code will prevail, which is the advantage of polymorphism. It improves the Code's organizational structure and readability, and guarantees scalability.
So how does the JVM point to play () in the Latin class? To solve this problem, JAVA uses the post-binding concept. When a message is sent to an object, during the compilation phase, the compiler only ensures the existence of the called method and checks the call parameters and return types, but does not know the exact code to be executed, the called Code cannot be determined until it is run. Take the above Code as an example. When JAVA is binding later, JVM will obtain the direct address of the Latin object from the Latin method table in the method area. This is why the real execution result is Latin. the reason for play. Before explaining the method table, let's take a look at the concept of binding.
Associating a method call with a method subject is called binding. in JAVA, it is divided into pre-binding and post-binding (dynamic binding or runtime binding ), binding before the program is executed (implemented by the compiler and the Connection Program) is called pre-binding, because the direct address of the called method during the compilation stage is already stored in the constant pool of the class to which the method belongs, it is called directly when the program is executed. For more information, see the final reference address. Later binding means to bind an object based on its type when the program is running. To bind an object later, you must have a mechanism to determine the object type during running and find the corresponding method, in short, some "type information" must be placed in the object. In JAVA, except the static method and final method (private method), other methods are post-bound. Later binding will involve an important data structure under JVM management-method table, the method table records the direct address of the visible method bytecode of the current class and all its parent classes in an array.
The specific call process of dynamic binding is as follows:
1. First, the fully qualified name of the class to which the called method belongs will be found.
2. find the called method in this method table. If found, the index items of this method in the method table will be recorded in the constant pool (this process is called constant pool resolution). If not, compilation failed.
3. Find the method table of the object in the method area based on the specific instantiated object, find the called method in the method table, and find the memory space of the bytecode through the direct address.
Finally, it is explained that both the domain and static methods are non-polymorphism, and any domain access operations will be parsed by the compiler, so they are not polymorphism. Static methods are associated with classes rather than a single object. For details about dynamic binding, please refer to the document link. I personally feel that the analysis is in place.
References: http://hxraid.iteye.com/blog/428891