-
Hajipur, Bihar, 844101
Polymorphism means "many forms". In Python, it refers to the ability to use a single function name or method name in multiple ways depending on the object or class that is using it.
The same function can behave differently based on the input type:
print(len("Python")) # 6 (string)
print(len([1, 2, 3])) # 3 (list)
print(len({"a": 1, "b": 2})) # 2 (dictionary)
✅ The function len()
works on different data types but behaves according to the type passed.
Different classes can have the same method name, but with different behavior.
class Dog:
def speak(self):
print("Woof!")
class Cat:
def speak(self):
print("Meow!")
for animal in (Dog(), Cat()):
animal.speak()
✅ The method speak()
exists in both classes but behaves differently.
You can use polymorphism with inheritance by overriding methods in child classes.
class Animal:
def sound(self):
print("Animal sound")
class Cow(Animal):
def sound(self):
print("Moo")
class Snake(Animal):
def sound(self):
print("Hiss")
for a in (Cow(), Snake()):
a.sound()
✅ The method sound()
is defined in the parent class but overridden in each child class.
You can pass different objects to a single function and get different behavior.
def call_speak(animal):
animal.speak()
call_speak(Dog()) # Woof!
call_speak(Cat()) # Meow!
Code becomes more flexible and maintainable.
Same method names reduce confusion.
Makes use of abstraction and inheritance effectively.
Q1. Write a Python program to create two different classes with a method named describe()
that prints different outputs.
Q2. Write a Python program to use a for
loop to call the same describe()
method from different class instances.
Q3. Write a Python program to override a method from a parent class in two different child classes and demonstrate the outputs.
Q4. Write a Python program to create a base class Vehicle
with a start()
method, then override it in Car
and Bike
classes.
Q5. Write a Python program to use a single function to call start()
on different objects like Car
and Bike
, demonstrating polymorphism.
Q6. Write a Python program to create a class Bird
with a method fly()
, then override it in Eagle
and Penguin
classes with different behavior.
Q7. Write a Python program to store different animal objects in a list and call a common method like make_sound()
for each.
Q8. Write a Python program to create a base class with a greet()
method and override it in Student
and Teacher
classes.
Q9. Write a Python program to demonstrate polymorphism using a function that takes any object with a run()
method.
Q10. Write a Python program to build a simple game setup with a base class Character
and different character types like Warrior
, Mage
, etc., each using polymorphic methods such as attack()
.
Q1: What does polymorphism mean in Python?
Q2: Which function shows built-in polymorphism in Python?
Q3: What is the benefit of polymorphism?
Q4: How can classes show polymorphism?
Q5: What happens when a method is overridden in a child class?
Q6: What must be common for polymorphism to work with different objects?
Q7: Can polymorphism be used without inheritance?
Q8: What keyword is used to override a method in Python?
Q9: What does speak() in both Dog and Cat class represent?
Q10: Which concept allows one function to accept different objects?