QamoosTech
Programming FundamentalsObject-oriented programmingIntermediatePythonTypeScript

Polymorphism

Pronunciationpol-ee-MOR-fiz-um

Definition

Polymorphism means different classes can respond to the same method call in their own way, so code can work with them without knowing their exact type.

Where you hear it

In object-oriented design discussions, interviews about the pillars of OOP, and code reviews that replace long if chains.

Examples

  • Each shape implements area(), so the report just calls it on every shape.
  • Thanks to polymorphism, adding a new payment type needs no change to the checkout code.
  • The checkout calls process() on every payment object, whatever its class.

Common mistake

Using type checks like if isinstance(...) everywhere instead of letting each class carry its own behaviour.

Don't confuse with

Inheritance, which is one way to get polymorphism by sharing a parent class. Polymorphism is the behaviour; inheritance is a tool.

Say it at work

  • In a meeting or chatLet's use polymorphism here instead of a long if-else on the type.
  • In an email or ticketEach handler overrides run(), so the loop doesn't care which one it has.