Here is a direct copy paste from wikipedia explaining xavi Lopez example:
"There are lots out there. For instance, this is a classical one: an Animal can makeSound(). A subclass of Animal called Dog will bark when implementing makeSound() and another one named Cat will meow. The makeSound() method behaves differently for an Animal depending on which is the actual subclass of the instance. – Xavi López"
abstract class Animal {
abstract String talk();
}
class Cat extends Animal {
String talk() { return "Meow!"; }
}
class Dog extends Animal {
String talk() { return "Woof!"; }
}
void lets_hear(Animal a) {
println(a.talk());
}
void main() {
lets_hear(new Cat());
lets_hear(new Dog());
}
As you can see, the animal has sever different extensions. So depending on animal type, a different thing will be printed.
The output looks like:
>>Meow!
>>Woof!