If I've cast a subclass as its superclass, and call a method that was overridden in the subclass, does it perform the overridden or original method?
问题 Consider: Dog is a subclass of Animal , and Dog overrides Animal.eat() Animal[] animals = getAllAnimals(); for (int i = 0; i < animals.length; i++) { animals[i].eat(); } If Animal.eat() is overriden by Dog.eat() , which one is called when the method is called from an identifier of type Animal ( animals[i] ?) 回答1: The subclass method will be called. That's the beauty of polymorphism. 回答2: The subclass will be the only method call, unless the subclass calls the superclass like this: class Dog {