Polymorphism in Python
• Polymorphism allows the same function to
behave differently based on the object.
What is Polymorphism?
• Polymorphism enables one interface to be
used for different data types.
• ✔ Same method name, different behavior
• ✔ Increases flexibility and reusability
Diagram: Polymorphism Concept
• Animal
• |
• |---- Dog (sound())
• |---- Cat (sound())
Duck Typing Example
• class Duck:
• def sound(self):
• return 'Quack'
• class Human:
• def sound(self):
• return 'Hello'
• for obj in (Duck(), Human()):
Runtime Polymorphism (Method
Overriding)
• class Vehicle:
• def speed(self):
• print('Average speed')
• class Car(Vehicle):
• def speed(self):
• print('Car speed: 120km/h')
• Vehicle().speed()
Key Takeaways
• - One interface, multiple implementations
• - Python supports polymorphism through duck
typing & inheritance
• - Makes code scalable and clean
Visual Polymorphism Diagram
Animal
[Link]() [Link]()