Python Classes
Python Polymorphism
Polymorphism concepts
Python Polymorphism
Polymorphism means "many forms". In Python, polymorphism allows us to use the same interface for different data types.
Method Overriding
Child classes can override parent class methods:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog()
cat = Cat()
print(dog.speak()) # Woof!
print(cat.speak()) # Meow!