Object-oriented three major features : encapsulation, inheritance, polymorphism. From a certain point of view, encapsulation and inheritance are almost always prepared for polymorphism. This is our last concept, but also the most important point of knowledge.
polymorphic definition : means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)
the technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.
the role of polymorphism: to eliminate the coupling relationship between types.
in reality, there are numerous examples of polymorphism. For example, if you press the F1 button, if the current pop-up in the Flash interface is the as 3 help document, if Word Help is currently popping up under Word, Windows Help and Support appears under Windows. The same event occurs on different objects and produces different results.
Three necessary conditions for polymorphic existence
First, to have inheritance;
Second, to have a rewrite;
The parent class reference points to the subclass object.
1. Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2. expandability (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3. Interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4. Flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5. Simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects.
Specific examples:
/** * Game class * * @author Joker.chan */ public static class PlayGame { protected void PlayGame () {System.out.println ( "Play Game" ); } }
/** @author Joker.chan * */ public static class Dota1 extends PlayGame {@Override prot ected void PlayGame () { // TODO auto-generated method stub s Uper .playgame (); System.out.println ( DOTA1 dish b! ); } }
/** @author Joker.chan * */ public static class Dota2 extends PlayGame {@Override prot ected void PlayGame () { // TODO auto-generated method stub s Uper .playgame (); System.out.println ( "DOTA2 b!" ); } }
/** * sub-category lol@author Joker.chan * * */Public Staticclassextends PlayGame { @Override protected void PlayGame () { // TODO auto-generated method stub Super. PlayGame (); System.out.println ("No play at all!") ); } }
Call Procedure:
/*** * Java three major features of polymorphism * *@authorJoker.chan *@paramargs **/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stubplaygame[] Games=NewPlaygame[3]; games[0] =NewDota1 (); games[1] =NewDota2 (); games[2] =NewLol (); for(inti = 0; i < games.length; i++) { if(Games[i]! =NULL) {games[i].playgame (); } } }
Output Result:
Play GameDOTA1 dish b! Play GameDOTA2 side Dishes B! Play game is not playing at all !
The polymorphism of three major features of Java