-
Polymorphism (polymorphism):
We say that the subclass is the parent class, so the polymorphic means that a reference to the parent type can point to the object of the child class
public class Polytest {public static void main (string[] args) {//TODO auto-generated method stub flower Rose = New Rose ()//polymorphic, subclass is parent class, parent type Reference can point to an instance of a subclass, or an object rose.sing ();}} Class Flower {public void sing () {System.out.println ("Flower is Sing");}} Class Rose extends flower{}
Method overrides can determine polymorphism, and method overloading cannot determine polymorphic
2. A reference to a parent type can point to a reference to a subtype, and when the parent class calls a method of a subclass, it must ensure that the current method is present in the subtype
Parent P = new Child (); When using polymorphic methods, first check if there is a sing () method in the parent class, if there is no mutation error, if any, then call the subclass's Sing () method
Following error type
public class Polytest {public static void main (string[] args) {//parent parent = new parent ();//parent.sing ();//child chil D = new Child ();//child.sing (); Parent p = new Child ();p. sing ();}} Class parent{/*public void Sing () {System.out.println ("Parent is Running");} */}class Child extends Parent{public void sing () {System.out.println (' child is running ');}}
3. Conversion of the downward type
public class PolyTest2 {public static void main (string[] args) {//TODO auto-generated method stub animal A = new Dog ();D og dog = (Dog) a;dog . sing ();}} Class animal{public void Sing () {System.out.println ("Animal is Sing");}} Class Dog extends Animal {public void sing () {System.out.println (' Dog is Sing ');}}
Polymorphism is the behavior of the run time, not the behavior of the compilation
3. There are two types of coercion type conversions:
1. Up-type conversion (UPCAST): For example, convert the cat type to the animal type and convert the subtype to the parent type. An upward type conversion does not need to be explicitly developed
2. Down type conversion (downcast): For example, convert the animal type to cat type. Converts the parent type to a subtype. For down-type conversions, you must explicitly specify (mandatory type must be used)
Package com.yuanzijian01;public class polytest2 {public static void main ( String[] args) {// TODO Auto-generated method stub/*animal a = new dog ();D Og dog = (Dog) a;dog.sing (); Animal b = new cat (); cat c = (Cat) b;c.sing (); *//*animal animal1= new cat (); Animal animal2= new animal (); animal2 = animal1;animal2.sing (); *//*animal animal1= new cat (); Animal animal2= new animal (); animal1 = animal2;animal1.sing (); *//*Cat cat = new cat (); Animal animal = cat;animal.sing (); *//*animal animal = new animal (); cat cat = (cat) animal; the wrong point, pointing to who is able to convert the */ //upward type conversion catcat = new cat (); animal animal = (Animal) cat;animal.sing ();//down type conversion animal a = new cat (); cat c = (Cat) a;c.sing ();}} Class animal{public void sing () {System.out.println ("animal is sing");}} Class dog extends animal {public void sing () {System.out.println ("Dog is sing ");}} Class cat extends animal{public void sing () {System.out.println ("cat is sing ");}}
Java Object-oriented polymorphism