1, definition: polymorphism is the same behavior has a number of different manifestations or forms of ability, that is, an interface of different instances to perform different operations;
2. Advantages: Eliminate the coupling relationship between types, replaceable, extensibility, interface, flexibility, simplification;
3.3 necessary conditions for polymorphic existence
A, inheritance: in polymorphism, there must be a subclass of inheritance and the parent class;
B, rewrite: Subclasses redefine some methods in the parent class, and the methods in the subclasses are called when they are called;
C, parent reference to child class object (UP): The child class reference needs to be assigned to the parent object in order to invoke the method of the parent class and the child class;
1 Public classTest {2 Public Static voidmain (String [] args) {3 Person pc = new Chinese (); //Parent class reference to child class object4Pc.sayhello ();//call Chinese's sayHello output--Hello5Person PA =NewAmerican ();//Upward Transformation6Pa.sayhello ();//Output-->hello7 }8 }9 Ten Public Abstract classPerson { One Public voidSayHello (); A } - - Public classChineseextendsPerson { the Public voidSayHello () - { -System.out.println ("Hello"); - } + } - + Public classAmericanextendsPerson { A Public voidSayHello () at { -System.out.println ("Hello"); - } -}
Note: When invoking a method in a polymorphic manner, first check that there is no such method in the parent class, there is no compile error, there is a way to call the subclass of the same name method (overriding the parent class method); Because the parent class reference to the subclass is transformed upward, it can only access properties and methods owned in the parent class, When you call these methods, you must use the methods in the subclass (virtual method calls);
A reference variable declared as a parent class type can only call a method in the parent class, and if the variable actually refers to a subclass object, and the subclass object overrides (Overrides) the method of the parent class, the parent class object calls the method in the subclass (to invoke the overridden method in the parent class, you must use the keyword Super ), this mechanism is called virtual method invocation .
4, multi-state Implementation mode: rewrite, interface, abstract class and abstract method;
5. Java encapsulation: by setting the public access method (getter and setter method) to the private property of the class, it hides the concrete implementation of the class and improves the security of the object data. You can also freely modify the internal implementation code if the external call is not changed;
1 Public classperson{2 3 PrivateString name;// private properties can only be accessed by this class, and other classes are not accessible, so the information is hidden4 ?5 PublicString GetName () {// public value method for private property external6 returnname;7 }8 ??9 Public voidSetName (String name) {// Private Property Public assignment method for externalTen This. Name =name; One } A}
Note: The public method is the entry for an external class to access the class member variable, and any class that accesses a private member variable in the class is passed through these getter and setter methods;
The This keyword is intended to resolve a conflict with the same name that occurs between the instance variable (private String name) and the local variable (name variable in setName (String name);
6. Java Overrides and overloads
Rewrite: Subclasses to the parent class to allow access to the method to rewrite the implementation, the return value and parameters can not be changed, that is, the shell is unchanged, core rewrite, overriding is a subclass of the parent class and a polymorphism of the expression ;
Overloading: In a class, the method name is the same, but the parameters are different, the return type can be the same or different, overloading is the polymorphism of a class performance ;
1 Public classPerson {2 Public voidSayHello () {3System.out.println ("SayHello");4 } 5 //overloaded SayHello Method6 Public voidSayHello (String name) {7SYSTEM.OUT.PRINTLN (name + "SayHello"));8 }9 }Ten One Public classChineseextendsPerson { A //overriding the parent SayHello method - Public voidSayHello () { -System.out.println ("Hello"); the } -}
Java Knowledge Point Grooming--polymorphism