Skip to main content

Command Palette

Search for a command to run...

Polymorphism in Python

Same Method, Different Behavior

Published
4 min read
Polymorphism in Python

If you’ve been following this series, you already learned about:

✔ Classes and Objects — how we create real-world entities in code
✔ Encapsulation — how we protect and control data
✔ Inheritance — how child classes reuse parent features

Now comes one of the most powerful and flexible concepts in Object-Oriented Programming:

👉 Polymorphism

Don’t worry if the word sounds complicated. The idea behind it is actually very simple — and once you understand it, your code starts to feel much smarter and more natural.


🌍 What is Polymorphism?

Let’s break the word first:

Poly = Many
Morphism = Forms

👉 Polymorphism means “one thing that can take many forms.”

In programming, this means:

The same method name can behave differently depending on the object using it.

Instead of writing completely different functions, we reuse the same method name — but each object responds in its own way.


🧠 Real-Life Example

Think about the action “speak.”

  • A Dog speaks → Bark 🐶

  • A Cat speaks → Meow 🐱

  • A Human speaks → Talk 🧑

Same action name — different behavior.

That’s exactly what polymorphism does in Python.


🧑‍💻 Basic Example — Method Overriding

Polymorphism most commonly appears through method overriding (which you saw a bit in inheritance).

class Animal:
    def speak(self):
        print("Animal makes a sound")

class Dog(Animal):
    def speak(self):
        print("Dog barks")

class Cat(Animal):
    def speak(self):
        print("Cat meows")

Now let’s create objects:

animals = [Dog(), Cat()]

for a in animals:
    a.speak()

Output:

Dog barks
Cat meows

👉 Notice something powerful:

We called the same method speak()
But each object behaved differently.

That’s polymorphism.


🔍 Why is Polymorphism Useful?

Imagine building a large system like a game or a school application.

Instead of writing:

dog_bark()
cat_meow()
human_talk()

You simply write:

object.speak()

Python automatically decides what to do.

Benefits:

✔ Cleaner code
✔ Less repetition
✔ Easier expansion
✔ Real-world behavior modeling


⚙️ Polymorphism Without Inheritance (Duck Typing)

Here’s something interesting about Python:

👉 Polymorphism doesn’t always need inheritance.

If different classes have the same method name, Python still treats them similarly.

class Car:
    def move(self):
        print("Car drives")

class Boat:
    def move(self):
        print("Boat sails")

class Plane:
    def move(self):
        print("Plane flies")

Now:

vehicles = [Car(), Boat(), Plane()]

for v in vehicles:
    v.move()

Output:

Car drives
Boat sails
Plane flies

Python doesn’t care about the class type —
it only cares whether the object has a move() method.

This concept is called Duck Typing:

“If it walks like a duck and quacks like a duck, it’s a duck.”


🔄 Method Overloading vs Method Overriding

Many beginners confuse these two, so let’s clarify clearly.

✔ Method Overriding

Happens in inheritance.

Child class replaces parent method behavior.

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

class Dog(Animal):
    def speak(self):
        print("Dog bark")

Same method name — new behavior.


⚠ Method Overloading in Python

Traditional overloading (same function name with different parameters like Java) is not directly supported in Python.

But Python achieves similar flexibility using:

  • Default arguments

  • *args

  • **kwargs

Example:

class Math:
    def add(self, a, b, c=0):
        print(a + b + c)

m = Math()
m.add(2, 3)
m.add(2, 3, 4)

Same method — different usage.


🎯 Real-World Analogy

Think about a remote control:

  • Press Play on TV → Video starts

  • Press Play on Music Player → Song plays

  • Press Play on Game Console → Game resumes

Same button.
Different behavior.

That’s polymorphism in everyday life.


🧪 Beginner Practice Questions

Try these after reading:

1️⃣ Animal Sounds Practice

Create:

  • Parent class Animal

  • Child classes Dog, Cat, Cow

Each should override a sound() method.


2️⃣ Shape Area Program

Create classes:

  • Circle

  • Rectangle

  • Triangle

Each should have a method area() that prints its area.

Call all objects inside a list and loop through them.


3️⃣ Transport Example (Duck Typing)

Create classes:

  • Bike

  • Car

  • Train

Each should have a move() method.

Call them using a loop without inheritance.


✨ Final Thoughts

Polymorphism makes your programs flexible and elegant.

Instead of writing different logic for every object, you define a common action — and let each class decide how to perform it.

If:

✔ Classes are blueprints
✔ Inheritance connects them

👉 Then polymorphism brings them to life by allowing dynamic behavior.


🔜 What’s Next?

Now that you understand:

  • Classes & Objects

  • Encapsulation

  • Inheritance

  • Polymorphism

You’re ready for the next pillar of OOP:

👉 Abstraction in Python — Hiding Complexity, Showing Only What Matters

Stay tuned for Article 6 🚀

Mastering OOP in Python

Part 5 of 6

In this series, I will guide you through the core concepts of OOP in Python from understanding classes, objects, constructors to master inheritance, polymorphism, encapsulation, abstraction. Articles includes examples, clear explanations, source code

Up next

Abstraction in Python

Hiding Complexity, Showing Only What Matters