Why bark method can not be called

前端 未结 5 851
醉梦人生
醉梦人生 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条回答
  •  时光取名叫无心
    2021-01-23 08:06

    You can call only the methods defined in the reference type. i.e since the Animal class has only one method ( run() ) you cant call bark() on it, even if its referring to Dog object.

    What you are doing is upcasting, you can get to know more about upcasting and downcasting here

提交回复
热议问题