What is The Example of Polymorphism in Python?

Polymorphism in Python allows objects of different classes to be treated as objects of a common base class.

It enables methods to be implemented in different ways for different classes while using a common interface.

Here’s an example to illustrate polymorphism in Python:

class Animal:
    def __init__(self, name):
        self.name = name
    
    def sound(self):
        pass


class Dog(Animal):
    def sound(self):
        return "Woof!"


class Cat(Animal):
    def sound(self):
        return "Meow!"


class Cow(Animal):
    def sound(self):
        return "Moo!"


# Polymorphic function
def make_sound(animal):
    print(animal.sound())


# Creating different animal objects
dog = Dog("Buddy")
cat = Cat("Whiskers")
cow = Cow("Molly")

# Making sounds using polymorphic function
make_sound(dog)  # Output: Woof!
make_sound(cat)  # Output: Meow!
make_sound(cow)  # Output: Moo!
Code language: Python (python)

In the above example, we have a base class Animal and three derived classes Dog, Cat, and Cow, each representing a specific animal.

The Animal class has a sound method that is overridden in each derived class to provide a unique sound for that animal.

We also have a polymorphic function make_sound that takes an Animal object as an argument and calls its sound method.

Regardless of the type of animal object passed to make_sound, it will call the appropriate sound method of that particular object, demonstrating polymorphism.

When we call make_sound with different animal objects (dog, cat, and cow), it invokes the overridden sound method of each object, resulting in different sounds being printed based on the object type.

Example 2

Here’s another example of polymorphism in Python:

class Shape:
    def calculate_area(self):
        pass


class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def calculate_area(self):
        return self.length * self.width


class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    
    def calculate_area(self):
        return 3.14 * self.radius ** 2


# Polymorphic function
def print_area(shape):
    print("Area:", shape.calculate_area())


# Creating different shape objects
rectangle = Rectangle(4, 6)
circle = Circle(5)

# Calculating and printing areas using polymorphic function
print_area(rectangle)  # Output: Area: 24
print_area(circle)  # Output: Area: 78.5
Code language: Python (python)

In this example, we have a base class Shape and two derived classes Rectangle and Circle, representing different shapes.

The Shape class has a calculate_area method, which is overridden in each derived class to calculate the area specific to that shape.

We also have a polymorphic function print_area that takes a Shape object as an argument and calls its calculate_area method.

Regardless of the type of shape object passed to print_area, it will invoke the appropriate calculate_area method of that particular object, demonstrating polymorphism.

When we create different shape objects (rectangle and circle) and pass them to the print_area function, it calculates and prints the area specific to each shape based on the object type.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment