JAVA polymorphism and java Polymorphism
/* Three major characteristics of multi-state OOP: encapsulation, inheritance, and Polymorphism: it can be understood as multiple forms of the existence of things, such as: animals: cats, dogs, pig: Men, female 1. polymorphism embodiment parent class reference pointing to subclass object 2. polymorphism premise (1) there must be a relationship between Class and Class, can be an inheritance relationship, it can also be the implementation relationship (2) there must be rewriting 3. The benefits of polymorphism greatly improve the scalability of the program 4. the disadvantages of polymorphism although it improves the scalability of the program, however, it can only be referenced by the parent class and accessed to the member polymorphism member variables in the parent class. Features: 1. During Compilation: see whether the referenced variable has a called method in the class to which it belongs. 2. runtime: see whether the called method exists in the class to which the object belongs * // Animal class Animal {void sing () {System. out. println ("singing") ;}// Dog class Dog extends Animal {void sing () {System. out. println ("Wangwang singing") ;}// Cat class Cat extends Animal {void sing () {System. out. println ("");} void catchMouse () {System. out. println ("rat hunting") ;}// Pig extends Animal {void sing () {System. out. println ("en sing") ;}} public class Test {public static void main (String [] args) {Cat c = new Cat (); function (c ); dog d = new Dog (); function (d); Pig p = new Pig (); function (p);} public static void function (Animal c) {c. sing ();}}