Java Section 35th polymorphism of three major features

Source: Internet
Author: User

2016-06-29

1 polymorphic
Polymorphism, which is actually a variety of states of an object
In our example,
Tiger can be seen as tiger, or as animal
Animal t=new Tiger ();
Tiger T1=new Tiger ();
Cat can be seen as a cat, or as a animal
Dog can be seen as a dog, or as a animal.

 Package com.java1995; /**  @author*/Publicclass  Animal        {public  void  showMe () {        System.out.println ("I am an animal");}    }
 package   com.java1995;  /**    @author   Administrator *  */ public  class  Cat extends   animal{ //  public void ShowMe () { //  System.out.println ("I Am a Cat");  //    
 package   com.java1995;  /**   sub-class: Puppy *   @author   Administrator *  */ public  class  Dog extends   animal{ public  void   ShowMe () {    System.out.println ( I am Puppy "
 Package com.java1995; /**  @author*/Publicclassextends  animal{         publicvoid  showMe () {        System.out.println ("I am a Tiger");}    }
 Packagecom.java1995;/*** Test class *@authorAdministrator **/ Public classTest { Public Static voidMain (string[] args) {//Animal a=new Animal ();//A.showme (); //Animal t=new Tiger ();//t.showme ();//        //Animal c=new Cat ();//c.showme ();        /*** polymorphic, multiple states of an object*/Animal T=NewTiger (); Animal C=NewCat (); Animal D=NewDog ();            ShowMe (d); }         Public Static voidshowMe (Animal a) {a.showme (); }}

Resources

[1] Java Easy Start Classic tutorial "full version"

Java Section 35th polymorphism of three major features

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.