polymorphic Continuation
A reference to a parent type can point to an object of a subtype.
1.package Com.pb.polytest;public class Polytest {public static void main (string[] args) {Parent p = new Parent ();p. sing (); }}class parent{public void Sing () {System.out.println ("Parent is Singing");}} Class Child extends Parent{public void sing () {System.out.println (' child is Singing ');}} Results show: Parent is Singing
-
package com.pb.polytest;public class polytest { Public static void main (String[] args) {child c = new child (); C.sing ();}} Class parent{public void sing () {System.out.println ("parent is singing");}} Class child extends parent{public void sing () {System.out.println ("child is Singing ");}} Results display: child is singing
Package Com.pb.polytest;public class Polytest {public static void main (string[] args) {Parent P = new Child ();//p is a parent type, pointing to a subclass So it prints Ch//ild is singingp.sing ();}} Class parent{public void Sing () {System.out.println ("Parent is Singing");}} Class Child extends Parent{public void sing () {System.out.println (' child is Singing ');}} Results show: Child is Singing
Parent P = new Child (); When calling a method using polymorphic mode , first check if there is a sing () method in the parent class, and if there is no compile error, then call the Sing () method of the subclass.
Package Com.pb.polytest;public class Polytest{public static void Main (string[] args) {Animal dog = new Dog ();d og.sing ();D o G D = (Dog) dog;d.sing (); Animal cat = new Cat (); cat.sing (); Cat C = (cat) cat;//cannot be written as dog D2 = (dog) cat, because cat references point to a Cat object, so the down conversion must be converted to Cat type c.sing ();}} Class animal{public void Sing () {System.out.println ("Animal is Singing");}} Class Dog extends animal{public void sing () {System.out.println ("Dog is Singing");}} Class Cat extends Animal{public void sing () {System.out.println ("Cat is Singing");}} Results show: Dog is Singingdog are Singingcat is Singingcat are singing
7. There are two types of mandatory type loading:
A) Up-type conversion (UPCAST): For example, convert the cat type to the animal type and convert the subtype to the parent type. For up-type conversions, you do not need to specify explicitly.
b) down type conversion (downcast): For example, convert the animal type to cat type, and convert the parent type to a subtype. For down-type conversions, you need to display the specified (mandatory type conversions must be used).
8. polymorphic Applications
1.package com.pb.polytest;public class polytest2 {public void run (BMW BMW) { Bmw.run ();} Public void run (QQ QQ) {Qq.run ();} Public static void main (String[] args) {polytest2 test = new polytest2 (); BMW B = NEW BMW ();//annoying test.run (b);//annoying qq qq = new qq ();//Annoying Test.run (QQ); /annoying}}class car{public void run () {System.out.println ("car is runing");}} Class bmw extends car{public void run () {System.out.println ("bmw is runing") ;}} Class qq extends car{public void run () {System.out.println ("qq is runing");}} 2.package com.pb.polytest;public class polytest2 {public void run (Car car) { Car.run (); }public static void main (String[] args)  {POLYTEST2  TEST = NEW&NBSp PolyTest2 (); BMW B = NEW BMW (); Test.run (b); QQ QQ = NEW QQ (); Test.run (QQ);}} Class car{public void run () {System.out.println ("car is runing");}} Class bmw extends car{public void run () {System.out.println ("bmw is runing") ;}} Class qq extends car{public void run () {System.out.println ("qq is runing");}}
This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1661054
Multi-State explanation