What is virtual method calling in java?

前端 未结 4 620
故里飘歌
故里飘歌 2020-12-01 13:05

Ive saw the next paragraph in some computer science test and i\'ll hope i can get here a good explanation of what it means because i googled it for an hour and can\'t find a

4条回答
  •  渐次进展
    2020-12-01 13:25

    we mean that in java applications the executed method is determined by the object type in run time

    interface Animal{
      public void eat();
    }
    
    
    class Person implements Animal{
       public void eat(){ System.out.println("Eating Food");}
    }
    
    
    
    class Eagle implements Animal{
       public void eat(){ System.out.println("Eating Snake");}
    }
    

    in main

    Animal animal = new Person();
    animal.eat(); //it will print eating food
    animal = new Eagle();
    animal.eat(); //it will print eating snake
    

提交回复
热议问题