Python Program to Implement Polymorphism

Introduction

Polymorphism is a key concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables methods to be used in different ways based on the object that is invoking them. Polymorphism allows for the design of flexible and reusable code because the same method can perform different tasks depending on the context.

In Python, polymorphism is typically demonstrated through method overriding and method overloading, although Python doesn’t support method overloading in the traditional sense (same method name with different parameter lists). Instead, it leverages dynamic typing and inheritance to achieve polymorphism.

This tutorial will guide you through creating a Python program that demonstrates polymorphism by defining multiple classes that implement the same method in different ways.

Example:

  • Classes: Dog, Cat
  • Common Method: sound()
  • Program Output:
    The dog barks
    The cat meows
    

Problem Statement

Create a Python program that:

  • Defines a class named Dog with a method sound that prints a specific message.
  • Defines a class named Cat with a method sound that prints a different message.
  • Implements a function that takes an object as an argument and calls its sound method.
  • Creates objects of both Dog and Cat classes and uses the function to demonstrate polymorphism.

Solution Steps

  1. Define the Dog Class: Use the class keyword to define a class named Dog.
  2. Add a Method to the Dog Class: Define a method named sound that prints a message specific to dogs.
  3. Define the Cat Class: Use the class keyword to define a class named Cat.
  4. Add a Method to the Cat Class: Define a method named sound that prints a message specific to cats.
  5. Implement a Function to Demonstrate Polymorphism: Define a function that accepts an object and calls its sound method.
  6. Create Objects and Demonstrate Polymorphism: Instantiate objects of both Dog and Cat classes and pass them to the function to demonstrate polymorphism.

Python Program

# Python Program to Implement Polymorphism
# Author: https://www.rameshfadatare.com/

# Step 1: Define the Dog Class
class Dog:
    # Step 2: Add a Method to the Dog Class
    def sound(self):
        print("The dog barks")

# Step 3: Define the Cat Class
class Cat:
    # Step 4: Add a Method to the Cat Class
    def sound(self):
        print("The cat meows")

# Step 5: Implement a Function to Demonstrate Polymorphism
def make_sound(animal):
    animal.sound()

# Step 6: Create Objects and Demonstrate Polymorphism
dog = Dog()
cat = Cat()

make_sound(dog)  # Calls sound method of Dog class
make_sound(cat)  # Calls sound method of Cat class

Explanation

Step 1: Define the Dog Class

  • The Dog class is defined using the class keyword. This class represents a dog.

Step 2: Add a Method to the Dog Class

  • The sound method is defined in the Dog class. It prints a message specific to dogs, indicating that the dog barks.

Step 3: Define the Cat Class

  • The Cat class is defined using the class keyword. This class represents a cat.

Step 4: Add a Method to the Cat Class

  • The sound method is defined in the Cat class. It prints a message specific to cats, indicating that the cat meows.

Step 5: Implement a Function to Demonstrate Polymorphism

  • The make_sound function accepts an object (in this case, either a Dog or Cat object) and calls its sound method. This function demonstrates polymorphism, as it can work with objects of different classes that share the same method name.

Step 6: Create Objects and Demonstrate Polymorphism

  • Objects dog and cat are created from the Dog and Cat classes, respectively.
  • The make_sound function is called with both dog and cat objects, demonstrating how the same function can invoke different methods based on the object type.

Output Example

Example Output:

The dog barks
The cat meows

Additional Examples

Example 1: Polymorphism with More Classes

# Define a Bird class
class Bird:
    def sound(self):
        print("The bird sings")

# Create objects of Dog, Cat, and Bird
dog = Dog()
cat = Cat()
bird = Bird()

# Demonstrate polymorphism with different classes
make_sound(dog)
make_sound(cat)
make_sound(bird)

Output:

The dog barks
The cat meows
The bird sings

Example 2: Polymorphism with Inheritance

# Define a parent class Vehicle
class Vehicle:
    def move(self):
        print("The vehicle moves")

# Define a child class Car that inherits from Vehicle
class Car(Vehicle):
    def move(self):
        print("The car drives")

# Define a child class Bike that inherits from Vehicle
class Bike(Vehicle):
    def move(self):
        print("The bike rides")

# Create objects of Car and Bike
car = Car()
bike = Bike()

# Demonstrate polymorphism with inherited classes
car.move()
bike.move()

Output:

The car drives
The bike rides

Example 3: Polymorphism in a Loop

# Define a Fish class
class Fish:
    def sound(self):
        print("The fish makes a blub sound")

# List of different animals
animals = [Dog(), Cat(), Bird(), Fish()]

# Demonstrate polymorphism using a loop
for animal in animals:
    make_sound(animal)

Output:

The dog barks
The cat meows
The bird sings
The fish makes a blub sound

Conclusion

This Python program demonstrates how to implement polymorphism, a powerful concept in object-oriented programming that allows for flexible and reusable code. Polymorphism enables a single interface to be used with different underlying data types, making it easier to manage and extend code. Understanding and applying polymorphism is essential for writing robust and scalable Python programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top