Java: polymorphic

Source: Internet
Author: User

Polymorphic:

  Can be understood as the various manifestations of the existence of things.

1, the manifestation of polymorphism
A reference to the parent class points to its own subclass object
A reference to a parent class can also accept its own subclass object

2, the precondition of polymorphism
Must be a relationship between a class and a class, or inherit, or implement an interface
There is usually a premise: there is an overlay

3. The benefits of polymorphism
The appearance of polymorphism improves the extensibility of the program

4, the drawbacks of polymorphism
Members of the parent class can only be accessed using a reference to the parent class

5, polymorphic applications

Transformation:

Upward transformation: You can point a reference to a class object to the parent class object

Down transformation: You can use the special features of subclasses, but only if the reference of the parent class is also an object of that subclass, that is, the reference to the subclass object is always changing

1 /*2 animal: Cat, Dog3 4 */5 6 Abstract classAnimal7 {8      Public Abstract voideat ();9 }Ten  One classCatextendsAnimal A { -      Public voidEat () -     { theSystem.out.println ("Eat fish"); -     } -      Public voidCatchmouse () -     { +System.out.println ("Catch the Mouse"); -     } + } A  at classDogextendsAnimal - { -      Public voidEat () -     { -System.out.println ("Chew Bones"); -     } in      Public voidWatchdoor () -     { toSystem.out.println ("housekeeping"); +     } - } the  * classPigextendsAnimal $ {Panax Notoginseng      Public voidEat () -     { theSystem.out.println ("Eat feed"); +     } A      Public voidGongdi () the     { +System.out.println ("Arch"); -     } $ } $  - classDuotaidemo - { the      Public Static voidMain (string[] args) -     {WuyiCat C =NewCat (); the         //c.eat (); - function (c); Wu c.catchmouse (); -  AboutDog d =NewDog (); $         //d.eat (); - function (d); - D.watchdoor (); -  AfunctionNewPig ()); +  theSystem.out.println ("Above is normal use, the following is Polymorphic"); -Animal ACat =NewCat ();//type promotion, upward transformation $ function (aCat); theCat Bcat = (cat) ACat;//force a reference to a parent class into a subclass type, down transformation the bcat.catchmouse (); the  theAnimal Adog =NewDog (); - function (adog); inAnimal Apig =NewPig (); the function (apig); the     } About  the      Public Static voidfunction (Animal a) the     { the a.eat (); +     } -}

Characteristics of polymorphic member functions:

 At compile time: see if the referenced variable belongs to a class that has a calling method, if so, the compiler passes, and vice versa;

At run time: see if there are methods called in the class to which the object belongs.

The simple summary is: member function in polymorphic call, compile look to the left, run to see the right

Characteristics of polymorphic member variables:

Both compile and run reference to the left (the class to which the reference variable belongs)

Characteristics of polymorphic static member functions:

Both compile and run reference left

Java: polymorphic

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.