Skip to content
AgileLogics
4 min read

Polymorphism

Prerequisites: Inheritance

Animal[] animals = { new Dog("Rex"), new Cat("Tom") };

foreach (Animal a in animals) {
    a.MakeSound(); // runs Dog's or Cat's version, whichever it is
}
Rex barks
Tom meows

Exactly as in Java, code written against the base type Animal automatically works correctly with any subtype, decided at runtime based on the object's actual type.