Talking about polymorphism,
Overview
We all know that Object-oriented has four basic features: abstraction, encapsulation, inheritance, and polymorphism. These four features can be summarized in this way. abstraction, encapsulation, and inheritance are the basis of polymorphism, and polymorphism represents abstraction, encapsulation, and inheritance. Polymorphism is a very important part of Java, so let's talk about Polymorphism Today ).
What is polymorphism
Polymorphism is called when different classes of Objects make different responses to the same message. Just as the bell rings, students in physical education go to the playground to stand up, and students in Chinese classes sit in the classroom.
The role of Polymorphism
In short, decoupling is used. In detail, polymorphism is the basis of the design pattern. It cannot be said that all design patterns use polymorphism, but a large part of the 23 types are based on polymorphism.
Three conditions for Polymorphism
1. Inheritance: There is an inheritance relationship
2. Rewrite: The subclass overrides the parent class method.
3. Upward Transformation: parent class references to subclass objects
Add the second point. Since polymorphism exists, you must have the condition that "subclass override parent class method" is required, the following three methods cannot demonstrate polymorphism (because they cannot be overwritten ):
1. static method, because the method modified by static belongs to the class, not to the instance
2. final method, because the method modified by final cannot be overwritten by the quilt class
3. private method and protected method. The former is because the method modified by private is invisible to the subclass. The latter is because although the method modified by protected can be seen by the quilt class or overwritten by the quilt class, but it cannot be referenced by external entities. How can we talk about polymorphism in a method that cannot be referenced by external entities?
For polymorphism, we can conclude that:
1. Use the reference of the parent class to point to the object of the subclass;
2. This reference can only call methods and variables defined in the parent class;
3. If a method in the parent class is rewritten in the subclass, the method in the subclass will be called when the method is called. (dynamic connection and dynamic call)
4. Variables cannot be overwritten. The "Override" concept is only applicable to methods. If the "Override" is applied to the variables in the parent class in the subclass, an error is reported during compilation.
Implementation form of polymorphism:
There are two forms of polymorphism in Java. Inheritance and interfaces
Polymorphism Classification
1. polymorphism during compilation, that is, method overloading. From the JVM perspective, this is a static dispatch)
2. Runtime polymorphism, that is, method rewriting. From the JVM perspective, this is a dynamic dispatch (dynamic dispatch)
This will be detailed when writing to JVM.
Several principles for analyzing Polymorphism
If we do not understand the principle of polymorphism, from the perspective of usage, we can sum up the following three sentences:
For example, we have a parent class, Father, and Children.
1. The upward transformation is automatic. That is, Father f = new Children () is automatic and does not require strong conversion.
2. The downward transformation should be stronger. That is, Children c = new Father () cannot be compiled and passed. Children c = (Children) new Father () must be required to let the parent class know which subclass it will convert.
3. The parent class references the Child class object. The Child class overrides the method of the parent class and calls the method of the parent class. The Child class actually calls the method of the parent class that the Child class 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. It is useful everywhere in the work and must be fully understood. In addition, this article is the first article published by my blog, hoping to write more better and better quality articles in the future.
Reprinted address:
Http://www.cnblogs.com/xrq730/p/4820237.html
Http://www.cnblogs.com/chenssy/p/3372798.html