Polymorphism of three object-oriented features -- Java note (7)
Polymorphism: the type of an object that has multiple forms of compilation is determined by the type used to declare the variable, the type of the runtime is determined by the object that is actually assigned to the variable. If the compilation type and runtime type are different, there will be a multi-state example: copy code 1 class Preson {2} 3 class Teacher extends Preson {4} 5 6 public class Demo {7 public static void main (String args []) {8 // This line of code will produce polymorphism 9 Preson p = new Teacher (); 10} 11} copy the code to implement the polymorphism mechanism: the reference variable of the parent class can point to the Instance Object of the subclass, and the method called by the program is dynamically bound at runtime, that is, the method that references the real instance object pointed to by the variable, that is, the method of the object that is running in the memory, instead of referencing the method polymorphism defined in the type of the variable: consider different subclass objects as the parent class type, the difference between objects of different subclass can be shielded. Exception: Write Common Code and make general programming to adapt to the changing requirements of the inherited relational polymorphism. Example: Copy code 1 // bird 2 class Brid {3 public void eat () {4 System. out. println ("generally birds eat worms"); 5} 6 7} 8 // Sparrow 9 class Sparrow extends Brid {10 public void eat () {11 System. out. println ("I want to eat worms"); 12} 13} 14 // e 15 class Eagle extends Brid {16 public void eat () {17 System. out. println ("I'm the air overlord, I want to eat meat"); 18} 19} 20 // for food 21 class Foraging {22/* public void feed (Sparrow sparrow) {23 spa Rrow. eat (); 24} 25 public void feed (Eagle eagle) {26 eagle. eat (); 27} 28 */29/** 30 * the above two methods can be improved to the following method, polymorphism can achieve the same effect 31 */32 // directly transfer the parent (BIRD) to 33 public void feed (Brid brid) {34 brid. eat (); 35} 36} 37 public class TestDemo {38 public static void main (String [] args) {39 Foraging foraging = new Foraging (); 40 41 Sparrow sparrow = new Sparrow (); 42/** 43 * this line of code shows the polymorphism 44*45 * when running, find its actual type method based on the actual type 46 */47 foragi Ng. feed (sparrow); 48 49 Eagle eagle = new Eagle (); 50 foraging. feed (eagle); 51 52/** 53 * Eagle eagle = new Eagle (); 54*55 * Dog eagle = new Eagle (); 56*57 * Sparrow sparrow = new Sparrow (); 58*59 * Dog sparrow = new Sparrow (); 60*61*62 * The declared type can also be changed, the implementation of this function will not change to 63 */64 65} 66}. Copy the code category: Polymorphism During classification, polymorphism during method overloading, and static method rewriting cannot be overwritten, but can inherit from the quilt class. Because method rewriting is related to objects, the static modification method is not related to object reference type conversion: upward Transformation: Small ---------> large (subclass ----------> parent class) automatic conversion: Assign the subclass object to the parent class variable for downward Transformation: Big ---------> small (parent class -----------> subclass) before forced conversion, a parent class object is forcibly converted to a subclass object. Before forced conversion, you must first determine the instanceof object type: instanceof format: object instanceof class indicates to determine whether the object belongs to the type of this class. Returned result: Boolean ps: if the object to be used for determination has no inheritance relationship with this type, instanceof polymorphism cannot be used when the method is called: When a variable of the reference type is declared as the parent class, but actually references the object of the subclass (polymorphism) in this case, the variable cannot access its own fields and methods in the subclass. If the subclass overrides the method of the parent class, the method accessed through the variable is actually a method combination of the subclass: a class contains the reference of another class. This relationship can be referred to as the include relationship or the "has a" relationship.