Polymorphic (i)

Source: Internet
Author: User

Definition: The various forms of existence of a certain class of things.
Example: Animals in cats, dogs.
Cats This object corresponds to the type of cat type
Cat x = new Cat ();
The cat is also a kind of animal, can also call the cat animal
Animal y = new Cat ();
Animals are the type of parent extracted from the specific things of cats and dogs.
Result: The parent type reference points to the child class object

The code reflects:

Abstract class Animal{abstract void Eat ();} Class Cat extends Animal{public void Eat () {System.out.println ("eat Fish");} public void Catchmouse () {System.out.println ("catch Mouse");}} Class Dog extends Animal{public void Eat () {System.out.println ("eat Bones");} public void Kanjia () {System.out.println ("housekeeping");}} -----------------------------------------class Duotaidemo {public static void main (string[] args) {function (new Cat ( )); Eat fish function (new Dog);//eat bone}public static void function (Animal a)//animal a = new Cat (); {a.eat ();}}

  

Polymorphic (i)

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.