MEPX
Chapter 4 of 10All chapters

Chapter 4 of 10

Polymorphism

One call, many implementations.

Programming to an interface

If several types provide the same method, calling code can work with any of them without knowing which. That is what removes long chains of type checks.

  • Adding a new type then needs no change to the code that uses them.
  • A switch on a type field is usually polymorphism waiting to be extracted.

Duck typing

Dynamic languages check at call time rather than by declaration: if it has the method, it works. Static languages catch mismatches earlier, at the cost of more ceremony.