Original: http://www.cnblogs.com/xrq730/p/4820237.html
Overview
We all know that object-oriented has four basic features: abstraction, encapsulation, inheritance, polymorphism. These four characteristics, summed up can be understood, abstract, encapsulation, inheritance is the basis of polymorphism, polymorphism is the expression of abstraction, encapsulation, inheritance. Polymorphism is a very important part of Java, so let's talk about polymorphism today (polymorphism).
What is polymorphic
Different classes of objects that respond differently to the same message are called polymorphic. Just like the bell rang, the students in the P.E. class went to the playground and stood up, and the students in the Chinese class sat in the classroom in a good way.
The role of polymorphism
Simply speaking is decoupling. In more detail, the polymorphism is the basis of the design pattern, and it cannot be said that all design patterns are used in polymorphism, but a large part of the 23 species is based on polymorphism.
Three conditions for polymorphic presence
1. Having an inheritance relationship
2. Subclasses overriding parent class methods
3. Parent class reference to child class object
To add the 2nd, since the polymorphic existence must have the "subclass rewrite parent Method" condition, then the following three types of methods are not able to show polymorphism (because cannot be overridden):
1, static method, because the method that is modified by the static belongs to the class, but not the instance of the
2, final method, because the final modified method cannot be overridden by the quilt class
3, private method and protected method, the former is because the method of private modification is not visible, the latter is because although the method can be protected modified quilt class to see, can also be overridden by the quilt class, but it cannot be referenced externally, A method that cannot be externally referenced, how can we talk about polymorphism?
Classification of polymorphic
1, compile-time polymorphism, that is, the method of overloading, from the JVM's point of view, this is a static allocation (static dispatch)
2, run-time polymorphism, that is, the method of rewriting, from the JVM's point of view, this is a dynamic dispatch (dispatch)
In this case, the JVM will be written in detail
Several principles for analyzing polymorphic problems
If you do not understand the principle of polymorphism, from the point of view of the use of personal learning, working experience of the past few years, summed up the polymorphism is nothing but three words:
For example, we have a parent class father that has a subclass children
1, the upward transformation is automatic. That is, father F = new children () is automatic and does not require a strong turn
2, the downward transformation stronger turn. That is, children C = new Father () is not compiled, it must be children C = (children) New Father () so that the parent knows which subclasses it is going to go to.
3, the parent class refers to the child class object, the subclass overrides the method of the parent class, calls the method of the parent class, and actually calls the method of the parent class that the subclass overrides. That is, father F = new children (), f.tostring () actually calls the ToString () method in children
Postscript
Polymorphism is the most basic and important concept in the Java language system, where work is useful and must be understood thoroughly. In addition, this article is my blog Garden hair of the first article, I hope to write more and better, more quality of the article.
Understanding of polymorphism in object-oriented