On this page

Inheritance

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

The Purpose of Inheritance​

Inheritance allows one class (the child or subclass) to acquire the attributes and methods of another class (the parent or base class). It is a core mechanism in object‑oriented programming that enables:

Inheritance lets you build specialized versions of existing classes while preserving shared functionality.

Defining a Subclass​

A subclass is defined by placing the parent class in parentheses.


class Animal:

    def speak(self):

        return "Some sound"



class Dog(Animal):

    pass

Here, Dog inherits everything from Animal.

Calling:


d = Dog()

d.speak()

works because Dog automatically gains the speak method from Animal.

Adding New Behavior in a Subclass​

A subclass may define additional attributes or methods.


class Dog(Animal):

    def wag_tail(self):

        return "Wagging tail"

Dog now has both:

This is the essence of extending a class.

Overriding Methods​

A subclass may replace (override) a method inherited from the parent class.


class Dog(Animal):

    def speak(self):

        return "Bark"

Now:


Dog().speak()   # "Bark"

The subclass version takes precedence.

Overriding allows specialization while keeping the overall structure intact.

Calling the Parent Version with super()​

Sometimes you want to extend, not replace, the parent behavior.

Use super() to access the parent implementation.


class Animal:

    def speak(self):

        return "Some sound"



class Cat(Animal):

    def speak(self):

        base = super().speak()

        return base + " (but softer)"

super() ensures proper cooperation in class hierarchies and avoids hard‑coding parent names.

Inheriting and Extending __init__​

Subclasses often need their own initialization while still using the parent’s setup.


class Animal:

    def __init__(self, species):

        self.species = species



class Dog(Animal):

    def __init__(self, name):

        super().__init__("Dog")

        self.name = name

This pattern ensures:

Multi‑Level Inheritance​

A class may inherit from a subclass, forming a chain.


class Animal:

    pass



class Mammal(Animal):

    pass



class Dog(Mammal):

    pass

Each level inherits from the one above it.

Python resolves attributes by searching upward through the chain.

Multiple Inheritance​

Python allows a class to inherit from more than one parent.


class Flyer:

    def fly(self):

        return "Flying"



class Swimmer:

    def swim(self):

        return "Swimming"



class Duck(Flyer, Swimmer):

    pass

Duck now has both fly and swim.

Multiple inheritance is powerful but should be used carefully to avoid complexity.

Method Resolution Order (MRO)​

When multiple inheritance is used, Python determines which parent to search first using the Method Resolution Order (MRO).

You can inspect it with:


Duck.__mro__

Python uses a consistent algorithm (C3 linearization) to ensure predictable behavior.

Best Practices for Inheritance​

These practices lead to clear, maintainable object‑oriented designs.

Summary​

In this lesson, you learned how inheritance enables classes to reuse and extend behavior. You explored subclass definitions, method overriding, super(), multi‑level inheritance, multiple inheritance, and the method resolution order. These concepts form the structural backbone of object‑oriented programming and prepare you for more advanced topics such as polymorphism and abstract classes.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Attributes & Methods

Next

Polymorphism