3 · Object-Oriented Core · lesson 16 of 41

Polymorphism

One interface, many runtime behaviors.

Java
Animal[] zoo = { new Dog(), new Cat(), new Cow() };
for (Animal a : zoo) a.speak();   // each speaks its own way
Key takeaways
  • ✓Program to the parent type; the JVM picks the right override.
  • ✓Enables 'open for extension, closed for modification' code.