Deep understanding of the Java polymorphism principle

Source: Internet
Author: User

Before always know what is polymorphic, usually hit the code is often used to polymorphic, but has not really understood the multi-state underlying operating mechanism in the end is how, these two genius research understand point, specifically written down, with your classmates progress together, but also hope that the great God guidance and correction.

  The concept of polymorphism: the same action works on different objects, can have different interpretations, and there are different execution results, which is polymorphism, which is simply: the reference of the parent class is directed to the subclass object . Let's take a look at some code.

1  Packagepolymorphism;2 3 classDance {4      Public voidPlay () {5System.out.println ("Dance.play");6     }7      Public voidPlayinti) {8System.out.println ("Dance.play" +i);9     }Ten } One  A classLatinextendsDance { -      Public voidPlay () { -System.out.println ("Latin.play"); the     } -      Public voidPlayCharc) { -System.out.println ("Latin.play" +c); -     } + } - classJazzextendsDance { +      Public voidPlay () { ASystem.out.println ("Jazz.play"); at     } -      Public voidPlayDoubled) { -System.out.println ("Jazz.play" +d); -     } - } -  Public classTest { in      Public voidperform (Dance Dance) { - Dance.play (); to     } +      Public Static voidMain (string[] args) { -         NewTest (). Perform (NewLatin ());//upcasting the     } *}
View Code

Execution result: Latin.play. This time you may find that the perform () method does not have a similar "if parameter type = Dance/latin" judgment, in fact, this is the polymorphism of the characteristics, it eliminates the coupling between the types, so that we can not treat an object as it belongs to the particular type of treatment, Instead, it is treated as the type of its base class. Because the test code above can be written exactly like this:

1  Public classTest {2      Public voidperform (Latin dance) {3 Dance.play ();4     }5      Public voidperform (Jazz dance) {6 Dance.play ();7     }8      Public Static voidMain (string[] args) {9         NewTest (). Perform (NewLatin ());//upcastingTen     } One}
View Code

However, you will find that if you add more new classes like perform () or new ones derived from dance, you will add a lot of work, and by comparison you will know that the first code will prevail, which is the advantage of polymorphism, which improves the organization and readability of the code while ensuring extensibility.

So how does the JVM point to play () in the Latin class? To solve this problem, Java uses the concept of late binding. When sending a message to an object, at compile time, the compiler guarantees only the existence of the called method, checks the invocation parameters and return types, but does not know the exact code to be executed, and the code that is called is not determined until run time. Take the above code example, Java in the late binding, the JVM will be in the method area of the Latin method table to take the Latin object's direct address, this is why the actual execution of the result is the reason for Latin.play, before interpreting the method table, we first understand the concept of binding.

Associating a method call with the same method body is called binding, and Java is divided into early binding and late binding (dynamic binding or runtime binding), binding before the program executes (implemented by the compiler and the linker), called early binding. Because the direct address of the called method at compile-time is already stored in the constant pool of the class to which the method belongs, the program executes directly, and the specific explanation please refer to the last reference address . Late binding means that when the program is running according to the type of object binding, want to implement late binding, you must have some mechanism, so that at run time can judge the type of object, so as to find the corresponding method, in short, must be placed in the object of a certain type of letter, Java in addition to the static method, The final method (private method belongs to), other methods are late-bound. Late binding involves an important data structure under the management of the JVM-the method table, which records the direct address of the current class and all of its parent class's visible method bytecode in memory as an array .

Dynamic binding the specific calling procedure is:

1. The fully qualified name of the class to which the called method belongs will be found first

2. Look for the called method in the method table of this class, if found, records the index entry of this method in the method table into the constant pool (this process is called Chang parsing), if not, the compilation fails.

3. Find the method table of this object in the method area according to the object of the specific instantiation, find the called method in the method table, and finally find the memory space where the bytecode resides through the direct address.

Finally, the domain and static methods are not polymorphic, and any domain access operations will be parsed by the compiler and therefore not polymorphic. A static method is associated with a class, not a single object. For dynamic binding and do not understand, please see the data link, personal feeling analysis is in place

Reference: http://hxraid.iteye.com/blog/428891

 

Deep understanding of the Java polymorphism principle

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.