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 methodsound
that prints a specific message. - Defines a class named
Cat
with a methodsound
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
andCat
classes and uses the function to demonstrate polymorphism.
Solution Steps
- Define the
Dog
Class: Use theclass
keyword to define a class namedDog
. - Add a Method to the
Dog
Class: Define a method namedsound
that prints a message specific to dogs. - Define the
Cat
Class: Use theclass
keyword to define a class namedCat
. - Add a Method to the
Cat
Class: Define a method namedsound
that prints a message specific to cats. - Implement a Function to Demonstrate Polymorphism: Define a function that accepts an object and calls its
sound
method. - Create Objects and Demonstrate Polymorphism: Instantiate objects of both
Dog
andCat
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 theclass
keyword. This class represents a dog.
Step 2: Add a Method to the Dog Class
- The
sound
method is defined in theDog
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 theclass
keyword. This class represents a cat.
Step 4: Add a Method to the Cat Class
- The
sound
method is defined in theCat
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 aDog
orCat
object) and calls itssound
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
andcat
are created from theDog
andCat
classes, respectively. - The
make_sound
function is called with bothdog
andcat
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.