// 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