โšกLLD Hub
Learn

Polymorphism โ€” One interface, many behaviours

One interface, many implementations โ€” callers stay dumb. ยท Avoid instanceof chains; dispatch through the interface.

Watch

Watch, then scroll down for code and practice.

In code

Polymorphic paymentTypeScript
interface Payment {
  authorize(amount: number): boolean;
}

class CardPay implements Payment {
  authorize(amount: number) {
    return amount < 10_000;
  }
}

class UpiPay implements Payment {
  authorize() {
    return true;
  }
}

function checkout(p: Payment, amt: number) {
  if (!p.authorize(amt)) throw new Error("declined");
}

๐Ÿ“˜ Key ideas

Runtime polymorphism

A parent reference holds a child object. The actual method called is determined at runtime โ€” this is dynamic dispatch.

The litmus test

If your code has instanceof checks or type-switch logic, your design is wrong. Good polymorphism means the caller never needs to know the concrete type.

Open for extension

Add new payment methods (Crypto, BNPL) without touching Checkout.processPayment() โ€” just add a new class that implements Payment.

Compile-time polymorphism

Method overloading โ€” same name, different parameter types. Less powerful but sometimes useful for convenience APIs.

๐Ÿง  Practice โ€” Apply What You Learned

๐Ÿš€ Now apply what you learned

Pick a problem above, write your solution, and get AI feedback on your design.

Start Practice โ†’