In java, Can we override a method by passing subclass of the parameter used in super class method?

后端 未结 6 1014
执念已碎
执念已碎 2020-12-11 15:48

As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class. What if we pass subclass of parameter wh

6条回答
  •  不思量自难忘°
    2020-12-11 16:09

    When you do this: Animal animal = new Dog(); it's upcasting.

    And you do not override the method

    public void eat(Flesh flesh){
        System.out.println("Dog eats "+ flesh);
    }
    

    so it calls the method eat(Food food) of Animal

提交回复
热议问题