9 min read Intermediate Lesson 4 of 9
Part 2 — Designing Systems

Polymorphism

Polymorphism ("many shapes") is the crowning achievement of object-oriented design. It allows caller code to interact with a uniform interface while different underlying classes execute distinct behaviors.

Eliminating Giant `if/elif` Switch Statements

def broadcast_alert(senders: list, message: str):
for sender in senders:
sender.send(message) # Dynamic Polymorphic Dispatch!

The broadcast_alert() function doesn't care whether a sender is an SMS, Email, Discord webhook, or PagerDuty alert. It just calls sender.send()!

Quick Summary

  • Polymorphism decouples high-level business logic from specific low-level implementations.
  • Next, we explore Abstraction & Abstract Base Classes.