This is done so that, given an
Animal
collection (containing different subclasses of
Animal
), the
animalSound
method can be invoked
through the Animal
class. It ends up invoking the actual implementation for whatever subclass of
Animal
is currently being used out of the collection. It also says that, to be an
Animal
, you have to implement the
animalSound
method.
EDIT: I see you were also asking how this is implemented. It's done with a
table of virtual methods[
^] that is associated with each class (not each
object, just each
class). Each
object has a
reference to the virtual method table associated with its
class, and that's how the correct method for the object is invoked.