Show in graph

Software → Software Design

Polymorphism

The ability for different implementations to be used through a shared interface or abstraction.

Brief explanation

Polymorphism means different objects can respond to the same operation in their own way. The caller depends on a common interface and does not need conditional logic for every concrete implementation.

Code example
Bad example
Python
class OrderService:  def checkout(self, total: float, payment_type: str) -> None:      if payment_type == "card":          print(f"Charging card: {total}")      elif payment_type == "paypal":          print(f"Charging PayPal: {total}")      else:          raise ValueError("Unsupported payment type")
Good example
Python
from typing import Protocolclass PaymentProcessor(Protocol):  def charge(self, amount: float) -> None:      ...class CardPaymentProcessor:  def charge(self, amount: float) -> None:      print(f"Charging card: {amount}")class PaypalPaymentProcessor:  def charge(self, amount: float) -> None:      print(f"Charging PayPal: {amount}")class OrderService:  def checkout(self, total: float, payment: PaymentProcessor) -> None:      payment.charge(total)