logo

Inheritance Basics

Inheritance lets you create a new class that builds on an existing one. The new class inherits all attributes and methods from the parent.

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

    def speak(self):
        pass

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

Dog inherits from Animal. It gets the __init__ method automatically and overrides speak with its own version.

buddy = Dog("Buddy")
print(buddy.name)    # Buddy (inherited)
print(buddy.speak()) # Woof! (overridden)

Inheritance avoids code duplication. Define common behavior once in a parent class, then specialize in child classes.

I cover inheritance deeply in my Python OOP course.