Python inheritance and polymorphism

A subclass can inherit behavior and replace a method with its own implementation. Calling the method uses the actual object's implementation.

Python
class Animal:
    def speak(self):
        return "sound"

class Dog(Animal):
    def speak(self):
        return "woof"

class Cat(Animal):
    def speak(self):
        return "meow"

for animal in [Dog(), Cat()]:
    print(animal.speak())
Read the full lesson