Application of Java polymorphism

Source: Internet
Author: User

// Application of polymorphism

class animal{

???? Public void eat () {

????}

}

class Dog extends animal{

???? Public void eat () {

???????? System. out. println (" The dog eats bones . ");

????}

}

class Cat extends animal{

???? Public void eat () {

???????? System. out. println (" The cat eats the mouse ");

????}

}

class Wolf extends animal{

???? Public void eat () {

???????? System. out. println (" Eat Pleasant goat ");

????}

}

class Test02 {

???? Public staticvoid main (String args[]) {

???????? Animal aa[]=new animal[3];

????????

???????? aa[0]=New Dog ();

???????? aa[1]=New Cat ();

???????? aa[2]=New Wolf ();

???????? For (int i=0;i<3;i++)

???????????? {aa[i].eat ();}

????}

}

/*

Experimental results:

The dog eats bones.

The cat eats the mouse

Eat Pleasant goat

?

Java There are two types of reference variables: compile-time type, which is determined by the type used to declare the variable

???????????????????????? The type of a run-time type, which is actually assigned to the variable

?

Polymorphic: The compile-time type is inconsistent with the run-time type and is called a polymorphic

???????????????????????? Is typically reflected between classes with inheritance relationships

???????????????????????? a parent class has more than one child class that can assign a subclass object to a parent class object without requiring any transformations

?

method Overrides: Subclasses transform methods of the parent class, called Method overrides ????????????????????????

????????????????????????

?

*/

Application of 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.