Explanation
- Polymorphism allows different classes to provide their own implementation of a method that is defined in a parent class. It allows you to use the same method name for different behaviors depending on the object that is calling it. Polymorphism can be achieved through **method overriding** in the subclass or **method overloading** (though Python doesn’t support method overloading directly).
**Key Points**:
- **Method Overriding**: A child class provides a specific implementation of a method that is already defined in the parent class.
- **Dynamic Method Resolution**: The correct method is determined at runtime based on the object’s type.
-
Steps
- Define a method in the parent class.
- Override the method in child classes to provide specific behaviors.
- Use the same method name, and the correct behavior will be chosen depending on the object type.
class Dog:
def sound(self):
print("Woof!")
class Cat:
def sound(self):
print("Meow!")
def make_sound(animal):
animal.sound()
dog = Dog()
cat = Cat()
make_sound(dog) # Output: Woof!
make_sound(cat) # Output: Meow!