Skip to main content

Polymorphism

The Hacker’s Shape‑Shifting Tool

Imagine you’re a hacker with a universal key. The same key can open different locks, each responding in its own way. That’s polymorphism in OOP: one interface, many implementations. It allows different classes to respond to the same method call uniquely, making systems flexible and extensible.

Polymorphism is the art of shape‑shifting code. Instead of writing separate logic for every type, you design a common interface and let each class define its own behavior.


Why Polymorphism Matters

  • Definition: Polymorphism means “many forms.” In OOP, it allows the same method name to behave differently depending on the object.
  • Reusability: Write code that works across multiple types without modification.
  • Extensibility: Add new classes without changing existing logic.
  • Flexibility: Systems adapt to new requirements easily.
  • Real‑World Analogy: Think of a “play” button. On a music player, it plays songs; on a video player, it plays movies. Same interface, different behavior.

Method Overriding

class Animal:
    def speak(self):
        print("Some sound")

class Dog(Animal):
    def speak(self):
        print("Woof!")

class Cat(Animal):
    def speak(self):
        print("Meow!")
animals = [Dog(), Cat()]
for a in animals:
    a.speak()
  • Why? The same speak() method behaves differently depending on the object.

Output:

Woof!
Meow!

Method Overloading

Python doesn’t support traditional method overloading, but you can simulate it with default parameters:

class Calculator:
    def add(self, a, b=0, c=0):
        return a + b + c

calc = Calculator()
print(calc.add(5))       # 5
print(calc.add(5, 10))   # 15
print(calc.add(5, 10, 20)) # 35
  • Why? One method adapts to different numbers of arguments.

Polymorphism with Functions

def call_speak(animal):
    animal.speak()

call_speak(Dog())  # Woof!
call_speak(Cat())  # Meow!
  • Why? Functions can operate on different object types as long as they share the same interface.

Real‑World Example

class Payment:
    def process(self):
        print("Processing payment...")

class CreditCard(Payment):
    def process(self):
        print("Processing credit card payment.")

class PayPal(Payment):
    def process(self):
        print("Processing PayPal payment.")
payments = [CreditCard(), PayPal()]
for p in payments:
    p.process()
  • Why? Same process() method, but each payment type implements it differently.

The Hacker’s Notebook

  • Polymorphism means one interface, many implementations. Method overriding lets child classes redefine parent behavior.
  • Python simulates method overloading with default parameters. Functions can operate polymorphically on different object types.

Hacker’s Mindset: treat polymorphism as your universal key. It unlocks flexibility, scalability, and adaptability in complex systems.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Jan 3, 2026