Multiple Inheritance in Python

Published Sept. 7, 2023, 11:01 a.m.

Multiple Inheritance in Python: Combining Features from Multiple Superclasses 🚀

Multiple Inheritance in Python is an amazing concept where a class can inherit features and methods from more than one superclass. Imagine a situation where a class not only inherits from its parents but also gets abilities from multiple different sources.

Here's a textual diagram to show the Multiple Inheritance situation:

🤖 Robot (🦕 Animal 🦕, 🚀 Machine 🚀)
┌─────────────────────────────┐
│        name                 │
│        species              │
│        brand                │
│        speak()              │
│        start()              │
└─────────────────────────────┘
       ^               ^
       |               |
       |               |
       |               |
       |               |
       v               v
🦕 Animal (Superclass)   🚀 Machine (Superclass)
┌────────────────────┐  ┌────────────────────┐
│   species          │  │   brand            │
│   speak()          │  │   start()          │
└────────────────────┘  └────────────────────┘

In this playful textual diagram:

  • 🤖 represents the Robot class, which is our main class inheriting from both Animal (🦕) and Machine (🚀) superclasses.
  • 🦕 and 🚀 represent the Animal and Machine superclasses, respectively.
  • Arrows show the direction of inheritance, with the Robot class inheriting from both Animal and Machine.

This diagram shows the Multiple Inheritance relationship in a fun way! 🤖🦕🚀

Let's see this with a detailed example:

# Superclass 1 - Animal
class Animal:
    def __init__(self, species):
        self.species = species

    def speak(self):
        pass  # Animals can make different sounds, so this method is left undefined

# Superclass 2 - Machine
class Machine:
    def __init__(self, brand):
        self.brand = brand

    def start(self):
        pass  # Starting a machine varies, so this method is left undefined

# Subclass - Robot, inheriting from both Animal and Machine
class Robot(Animal, Machine):
    def __init__(self, name, species, brand):
        # To correctly initialize features from both superclasses
        Animal.__init__(self, species)
        Machine.__init__(self, brand)
        self.name = name

    def speak(self):
        print(f"{self.name}, the {self.species}, makes beeping sounds")

    def start(self):
        print(f"{self.brand} robot {self.name} starts")

# Create a Robot object
r2d2 = Robot("R2-D2", "astromech droid", "AstroTech")

# Let's see what R2-D2 can do
r2d2.start()  # Starting up R2-D2
r2d2.speak()  # R2-D2's unique way of speaking

In this example, we have a superclass Animal representing creatures with species-specific behaviors and a superclass Machine representing machines from various brands. The Robot class inherits from both Animal and Machine, showing multiple inheritance.

Here's an explanation:

  • Animal has an __init__ method to initialize the species and a placeholder speak method.
  • Machine has an __init__ method to initialize the brand and a placeholder start method.
  • Robot inherits from both Animal and Machine. In its __init__ method, it calls the __init__ methods of both superclasses to correctly initialize their features.
  • Robot overrides the speak and start methods to provide unique behaviors for the robot.

When we create an object of the Robot class, such as r2d2, it can both speak like an animal and start up like a machine, demonstrating the power of multiple inheritance.