Menu
Coddy logo textTech

Multiple Inheritance

Part of the Object Oriented Programming section of Coddy's Python journey — lesson 20 of 64.

Multiple inheritance allows a class to inherit from more than one parent class, combining functionality from different sources.

Here are two parent classes:

class Animal:
    def __init__(self, name):
        self.name = name
    
    def eat(self):
        return f"{self.name} is eating"

class Flyable:
    def fly(self):
        return f"{self.name} is flying"

Create a child class that inherits from both parents:

class Bird(Animal, Flyable):
    def __init__(self, name, species):
        super().__init__(name)  # Calls Animal's __init__
        self.species = species
    
    def sing(self):
        return f"{self.name} is singing"

The syntax class Bird(Animal, Flyable): means Bird inherits from both Animal and Flyable.

Create a bird object and use methods from both parents:

sparrow = Bird("Sparrow", "House sparrow")

Call methods from the first parent class:

print(sparrow.eat())

Call methods from the second parent class:

print(sparrow.fly())

Call the bird's own method:

print(sparrow.sing())

Output:

Sparrow is eating
Sparrow is flying
Sparrow is singing

You can check the inheritance order:

print(Bird.__mro__)  # Method Resolution Order

This shows which parent gets checked first when looking for methods.

Key Point: Multiple inheritance uses class Child(Parent1, Parent2): syntax. The child class gets all methods from all parent classes. Python checks parents from left to right when looking for methods.

challenge icon

Challenge

Medium

In this challenge, you'll implement a multi-file class system demonstrating multiple inheritance with smartphones. Each class is organized in its own file for better code organization.

You'll need to edit these files:

  • device.py - Implement the Device base class
  • internet.py - Implement the Internet base class
  • smartphone.py - Implement the Smartphone class that inherits from both base classes
  • driver.py - Implement test cases that will verify your implementation works correctly

Each file contains detailed TODO comments that will guide you step-by-step through the implementation.

Cheat sheet

Multiple inheritance allows a class to inherit from more than one parent class using the syntax class Child(Parent1, Parent2):

class Animal:
    def __init__(self, name):
        self.name = name
    
    def eat(self):
        return f"{self.name} is eating"

class Flyable:
    def fly(self):
        return f"{self.name} is flying"

class Bird(Animal, Flyable):
    def __init__(self, name, species):
        super().__init__(name)  # Calls Animal's __init__
        self.species = species
    
    def sing(self):
        return f"{self.name} is singing"

The child class inherits all methods from all parent classes:

sparrow = Bird("Sparrow", "House sparrow")
print(sparrow.eat())   # From Animal class
print(sparrow.fly())   # From Flyable class
print(sparrow.sing())  # Bird's own method

Check the Method Resolution Order (MRO) to see which parent gets checked first:

print(Bird.__mro__)  # Shows inheritance order

Try it yourself

# TODO: Import the Smartphone class from smartphone.py

# Create a smartphone and test its methods
# TODO: Create a Smartphone object with brand "Apple" and model "iPhone 13"
my_phone = None

# TODO: Call and print the power_on() method of the smartphone

# TODO: Call and print the connect() method of the smartphone

# TODO: Call and print the make_call() method of the smartphone with parameter "123-456-7890"
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming