Python Polymorphism


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.


🔹 Built-in Polymorphism Example

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.


🔹 Polymorphism with Classes

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.


🔹 Polymorphism with Inheritance

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.


🔹 Polymorphism with Functions and Objects

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!

🔹 Why Use Polymorphism?

  • Code becomes more flexible and maintainable.

  • Same method names reduce confusion.

  • Makes use of abstraction and inheritance effectively.


Practice Questions

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().


Python Polymorphism Quiz

Q1: What does polymorphism mean in Python?

A. Using many classes at once
B. Having multiple constructors
C. Same method used in different ways
D. Using one variable name repeatedly

Q2: Which function shows built-in polymorphism in Python?

A. type()
B. id()
C. len()
D. print()

Q3: What is the benefit of polymorphism?

A. Code becomes slower
B. Code becomes complex
C. Code becomes more flexible
D. Code becomes private

Q4: How can classes show polymorphism?

A. Using only private variables
B. Having the same method name in different classes
C. Using different file names
D. Creating functions inside functions

Q5: What happens when a method is overridden in a child class?

A. Parent method is removed
B. Error occurs
C. Child method is used
D. Both methods run

Q6: What must be common for polymorphism to work with different objects?

A. Same variable types
B. Same file
C. Same method name
D. Same property name

Q7: Can polymorphism be used without inheritance?

A. Yes
B. No
C. Only with abstract classes
D. Only in multiple inheritance

Q8: What keyword is used to override a method in Python?

A. override
B. new
C. def (same name)
D. change

Q9: What does speak() in both Dog and Cat class represent?

A. Inheritance
B. Looping
C. Polymorphism
D. File handling

Q10: Which concept allows one function to accept different objects?

A. Inheritance
B. Recursion
C. Polymorphism
D. Debugging

Go Back Top