About Java polymorphism

Source: Internet
Author: User

What is polymorphic

The same implementation interface that uses different instances to perform different operations

A rule that converts a subclass into a parent class:

* When a reference to a parent class is directed to a subclass object, referred to as up transformation, automatic type conversion

* The method that is called by the parent class reference variable is a subclass that overrides or inherits the parent class's method, not the parent class's method

* You cannot call a subclass-specific method by referencing a variable from the parent class

/** pet, Dog and Penguin parent class*/Package cn.yy.test01; Public Abstract classPet {PrivateString name; Private intHealth ; Private intLove ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGethealth () {returnHealth ; }     Public voidSethealth (intHealth ) {         This. Health =Health ; }     Public intGetlove () {returnLove ; }     Public voidSetlove (intLove ) {         This. Love =Love ; }         Public Abstract voideat ();}
Package cn.yy.test01;/** * * Dog class * @author CKW **/ Public classDog extends Pet {PrivateString strain;//Variety             PublicString Getstrain () {returnstrain; }     Public voidSetstrain (String strain) { This. Strain =strain; }     PublicDog (String name,string strain) {super ();  This. Strain =strain; }     PublicDog () {super (); //TODO auto-generated Constructor stub    }    /** * to achieve a dog meal method **/@Override Public voideat () {}}
Package cn.yy.test01;/** * Penguin class * @author CKW **/ Public classPenguin extends Pet {PrivateString sex;//Sex         PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }    /** * To achieve the Penguin meal method*/@Override Public voideat () {//TODO auto-generated Method Stub    }}
Package cn.yy.test01;/** * Lord Human * @author CKW **/ Public classMaster {PrivateString name="";//Master Name    Private intmoney=0;//Number of ingot         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetmoney () {returnMoney ; }     Public voidSetmoney (intMoney ) {         This. Money =Money ; }     PublicMaster (String name,intMoney ) {         This. name=name;  This. money=Money ; }        /** The owner feeds the pet*/     Public voidFeed (Pet Pet) {pet.eat (); }        //The owner feeds the dog .     Public voidFeed (dog dog) {dog.eat (); }        //The host feeds Penguin .     Public voidFeed (Penguin pgn) {pgn.eat (); }            }
Package cn.yy.test01; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDog dog=NewDog ("even even","Snow Rena"); //Penguin penguin=new Penguin ();Master master=NewMaster ("Mr. Wang", -); Master.feed (dog);//The owner feeds the dog .            }}

About Java polymorphism

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.