Multi-State explanation

Source: Internet
Author: User

polymorphic Continuation

  1. A reference to a parent type can point to an object of a subtype.

  2. 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
  3. 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 


  4. 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
  5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.