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]
?)
The subclass method will be called. That's the beauty of polymorphism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With