Skip to content
techsnow

Object-Oriented Python

Inheritance

A class can inherit from another, reusing its code and adding or changing behaviour.

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "..."

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

for pet in [Dog("Moti"), Cat("Billo")]:
    print(pet.name, "says", pet.speak())

Output

Moti says Woof!
Billo says Meow!