2.8.1.6 Polymorphism
Python Programming
Polymorphism refers to the ability of different objects to respond to the same method or function call in their own unique way. It allows one interface to be used for a general class of actions, with specific implementations based on the object.
This is useful for calling methods in a uniform way, even if different classes implement them differently.
python
def animal_sound(animal):
animal.make_sound( )
dog = Dog( )
cat = Cat( )
animal_sound(dog) # Output: Bark!
animal_sound(cat) # Output: Meow!Explanation: In the above code, animal_sound() can handle objects of different classes (Dog and Cat) uniformly.