Why bark method can not be called

前端 未结 5 897
醉梦人生
醉梦人生 2021-01-23 07:20
class Animal{
    void run() {
    }
}
class Dog extends Animal {
    void bark() {
    }
}
class Testing{
    public static void main(String[] args)  {
        Animal d         


        
5条回答
  •  猫巷女王i
    2021-01-23 07:51

    It's because you store your dog in the variable of type Animal which is only able to run(). There could be another animal Cat which isn't able to bark().

    If you want to let the dog bark() then you need to put in a Dog typed variable:

    Dog rolf = new Dog();
    rolf.bark();
    

提交回复
热议问题