A summary of the polymorphism of the three features of Java
Three characteristics of object-oriented: encapsulation, inheritance and polymorphism. From a certain point of view, encapsulation and inheritance are almost always prepared for polymorphism. This is our last concept and the most important knowledge point.
1. Definition:
Polymorphism: it means that different objects are allowed to respond to the same message. That is, the same message can adopt many different behavior modes according to different sending objects. (sending a message is a function call)
2. The technology to realize polymorphism is called dynamic binding, which means to judge the actual type of the referenced object during execution and call its corresponding methods according to its actual type.
3. Function: eliminate the coupling relationship between types.
4. In reality, there are numerous examples of polymorphism. For example, if you press F1, the help document of AS3 will pop up in the flash interface; If word help pops up under word; Windows help and support will pop up under windows. When the same event occurs on different objects, it will produce different results.
5. Here are three necessary conditions for the existence of polymorphism, which require everyone to recite when dreaming!
Three necessary conditions for the existence of polymorphism
1、 There should be inheritance;
2、 There should be rewriting;
3、 A parent class reference points to a child class object.
6. Benefits of polymorphism:
1) Substitutability: polymorphic pairs are replaceable for existing code. For example, polymorphic pairs work for the circle class, as do any other circular geometry, such as a ring.
2) Extensibility: polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes. In fact, adding new subclasses is easier to obtain polymorphism functions. For example, it is easy to add sphere class polymorphism on the basis of realizing cone, semi cone and hemisphere polymorphism.
3) Interface capability: polymorphism refers to that a superclass provides a common interface to a subclass through method signature, and the subclass improves or overwrites it, as shown in Figure 8.3. The superclass shape in the figure specifies two interface methods that implement polymorphism, computerarea() and computevolume(). Subclasses, such as circle and sphere, improve or cover these two interface methods in order to achieve polymorphism.
4) Flexibility: it reflects flexible and diverse operations in applications and improves use efficiency.
5) Simplification: polymorphism simplifies the coding and modification process of application software, especially when dealing with the operation and operation of a large number of objects.
Cat and dog case code
7. Implementation of polymorphism in Java: interface implementation, method rewriting by inheriting the parent class, and method overloading in the same class.
8. Classification of polymorphism in Java:
In Java, polymorphism can be roughly divided into the following situations:
1) Person is the parent class and student is the child class. Then: personp = newstudent();
2) Fliablef = newbird(), if fliablef is the interface and bird is the class that implements the interface;
3) If fliable is an abstract class and bird is a class that inherits fliable, then: fliablef = newbird();
When polymorphic, it should be noted that P is declared as a reference of the parent class, but it is actually a reference of the child class. But he can only call methods in the parent class. If the method in the subclass overrides the parent method, Then the parent method will be called (virtual method call). Interface polymorphism is the same. You may ask, if f wants to call its own method, isn't it an error? In fact, this is also a method override, because subclasses implementing the interface will certainly implement the methods in the interface, so the methods in bird are called in this case. However, if a method in bird is not defined in the interface, f doesn't Can call.
9. Instanceof operator:
The polymorphism mechanism of Java language leads to the inconsistency between the declared type of reference variable and the type of its actual reference object. Combined with the virtual method call rules, it can be concluded that two reference variables declared as the same type may also have different behaviors when calling the same method. The instanceof operator is introduced here.
So if I declare personp = newstudent (); can I convert p to student? Of course, but I have to force conversion (if a son wants to be a father, come directly, and if a father wants to be a son, you have to force it).
It is usually judged by adding instanceof when casting.
if(pinstanceofstudent){students=(student)p;}
Polymorphism runs through the whole learning of Java. For example, when writing catch statements in exception handling, we stipulate that subclass exceptions must be written before and parent exceptions must be written after. Why? The reason is polymorphism. Our catch statement format: catch (exception E). When an exception is generated by a java program, an exception object will be automatically generated. If a subclass exception is generated first and the parent exception is written first, the catch statement will be executed according to polymorphism, and will jump out after executing a catch statement.
10. Examples:
Although I don't know much about Java polymorphism, the following example makes me understand some:
(3) Answer
****There is an answer from a kind man****
There are two key points to this problem:
One is the relationship between subclasses and parent classes, and the other is the call of overloaded methods.
Subclass objects can be used directly as parent objects, but vice versa. For example, people are the parent class and students are the children of people. Therefore, student objects must have the attributes of human objects, but human objects may not have the characteristics of student objects. Therefore, the student object can be used as a human object, but the human object can not be used as a student object. Note that when a subclass object is used as a parent object, the subclass object will lose all subclass properties and only retain the properties and methods with the same name as the parent class (the methods with the same name not only have the same function name, but also have the same parameter type, otherwise they will not be retained).
If an overloaded method is defined in a class, the system will automatically choose to call the appropriate method according to the type of parameter when calling the method.
1)a1. Shows (b). In a, there is no method with class B parameters, but the method with class a parameters is called according to the principle that the parent class of the subclass object is available
publicStringshow(Aobj)... {return("AandA");}
2)a1. Show (c). Class C is a subclass of class B, and class B is a subclass of class A. therefore, class C objects can be used as class a objects. The results are the same as above.
3)a1. Show (d), directly call the method in a according to the parameter type
publicStringshow(Dobj)... {
return("AandD");}
4)a2. Show (b), A2 is originally a B object, but it is assigned to a class a variable, so A2 only retains the properties and methods with the same name as parent class A. a2. Show (b) call the reserved coparametric method with the same name as the parent class in class B
public String show(A obj)... { return ("B and A"); }
5) a2. Show (c). There are no class C parameter methods in the reserved methods of class B, but there are parameter methods of parent class B containing C, so the called method public string show (a obj) { return ("B and A"); }
I think this explanation is more reasonable: A2 is originally an object of class B, but it assigns the value to class A, C is a subclass of B, and B is a subclass of A. therefore, A2 retains the attributes and methods with the same name as a in class B.
6) a2. Show (d), calling public string show (d obj) in class A { return ("A and D"); }
7) B. show (b), call the
public String show(B obj)... { return ("B and B"); }
8) B. show (c). There are no methods with Class C parameters in class B, but there are methods with class B parameters, so the method is called
public String show(B obj)... { return ("B and B"); }
9) B. show (d), the same as 8
summary
The above is the summary of Java polymorphism in this paper. I hope it will be helpful to you. If you have any questions, you can leave a message at any time and look forward to your valuable comments!