Home > Back-end >  Under the Java transformation question answered for bosses
Under the Java transformation question answered for bosses

Time:10-03

The class Animal
{
Public void eat ()
{
System. The out. Println (" ways of eating food ");
}
}
The class Hawks extends Animal
{
Public void eat ()
{
System. The out. Println (" eagle mouth diao ");
}
}
The class frog extends Animal
{
Public void eat ()
{
System. The out. Println (" frog with the tongue ");
}
}
The class locust extends Animal
{
Public void eat ()
{
System. The out. Println (" locusts attack with a group of ");
}
}
The class Main
{
Public static void main (String [] args)
{
Animal Animal=new Hawks ();//parent class calls a subclass object
Hawks hawks1=(complete with pocket) animal; (what is the meaning of the meaning of this sentence)
Hawks1. Eat ();

}
}

CodePudding user response:

Hawks hawks1=(complete with pocket) animal;//animal object cast to complete with pocket type, and the conversion result is assigned to hawks1 variable
This transformation is down to call their own special subclass method (that is, not inherited from the parent class method), in this case you eat methods inherited from the parent class (subclass is rewriting, and no new method), so meaningless downward transition, direct call animal. Eat () is the same as a result,


  • Related