On this page

Polymorphism

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What Polymorphism Means in OOP​

Polymorphism allows different objects to respond to the same operation in ways appropriate to their specific type.

The term comes from Greek: poly (many) + morph (form).

In programming, it means:

Polymorphism is a direct consequence of inheritance and method overriding, but it also works without inheritance as long as objects implement the same behavior.

Method Overriding as Polymorphism​

When a subclass overrides a method from its parent class, each subclass provides its own version of the behavior.


class Animal:

    def speak(self):

        return "Some sound"



class Dog(Animal):

    def speak(self):

        return "Bark"



class Cat(Animal):

    def speak(self):

        return "Meow"

Using polymorphism:


animals = [Dog(), Cat(), Animal()]



for a in animals:

    print(a.speak())

Each object responds according to its own implementation.

The loop does not need to know the specific type of each object.

Polymorphism Without Inheritance (Duck Typing)​

Python supports duck typing, a dynamic form of polymorphism.

If an object implements the required method, it can be used—regardless of its class hierarchy.


class Duck:

    def speak(self):

        return "Quack"



class Person:

    def speak(self):

        return "Hello"



def make_speak(obj):

    return obj.speak()

Both objects work:


make_speak(Duck())

make_speak(Person())

Python focuses on behavior, not type.

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

Polymorphism Through Common Interfaces​

A common interface is a set of method names shared across classes.

This is a powerful design technique for building interchangeable components.

Example: a drawing system with different shapes.


class Circle:

    def draw(self):

        return "Drawing a circle"



class Square:

    def draw(self):

        return "Drawing a square"

Using them polymorphically:


shapes = [Circle(), Square()]



for s in shapes:

    print(s.draw())

Each object implements the same interface (draw), so they can be used uniformly.

Polymorphism in Real‑World Design​

Polymorphism is essential in many real‑world scenarios:

Polymorphism allows systems to grow without rewriting existing code.

Designing for Polymorphism​

To take advantage of polymorphism:

These principles lead to flexible, extensible designs.

Summary​

In this lesson, you learned how polymorphism enables different objects to respond to the same operation in their own way. You explored method overriding, duck typing, common interfaces, and practical design strategies. Polymorphism is a cornerstone of object‑oriented programming and allows you to build systems that are modular, extensible, and easy to maintain.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Inheritance

Next

Encapsulation