LeetCampus
Interview Question

Does Python supports multiple Inheritance?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Does Python support multiple inheritance? is a fundamental question that tests a candidate's understanding of object-oriented programming (OOP) principles in Python. Interviewers ask this to evaluate a candidate's knowledge of class relationships, especially in complex systems where multiple inheritance could be beneficial or problematic. Multiple inheritance allows a class to inherit attributes and methods from more than one parent class, which can lead to powerful design patterns but also introduces challenges such as the Diamond Problem. Understanding how Python handles multiple inheritance through its Method Resolution Order (MRO) is crucial. This knowledge is not only relevant for coding but also for designing robust applications that effectively utilize inheritance. Candidates should be aware of common misconceptions, such as the belief that multiple inheritance is inherently bad; while it can lead to complications, it can also offer elegant solutions when used judiciously. Overall, this question assesses both theoretical knowledge and practical experience in software engineering, particularly in Python programming.

Sample Answers

Example 1: Direct Implementation of Multiple Inheritance

In Python, you can implement multiple inheritance by simply specifying multiple parent classes in the class definition. For example:

class Parent1:
    def greet(self):
        return 'Hello from Parent1!'

class Parent2:
    def greet(self):
        return 'Hello from Parent2!'

class Child(Parent1, Parent2):
    pass

child_instance = Child()
print(child_instance.greet())  # Output: 'Hello from Parent1!'

In this example, the Child class inherits from both Parent1 and Parent2. When we call greet(), the method from Parent1 is executed first due to Python's MRO. This showcases how multiple inheritance works in Python, enabling the child class to leverage functionality from multiple sources.

Example 2: Handling the Diamond Problem

The Diamond Problem arises in multiple inheritance when two parent classes inherit from a common ancestor. For instance:

class Base:
    def greet(self):
        return 'Hello from Base!'

class Parent1(Base):
    def greet(self):
        return 'Hello from Parent1!'

class Parent2(Base):
    def greet(self):
        return 'Hello from Parent2!'

class Child(Parent1, Parent2):
    pass

child_instance = Child()
print(child_instance.greet())  # Output: 'Hello from Parent1!'

Here, Child inherits from both Parent1 and Parent2, which both inherit from Base. Python resolves this by following the MRO, which prioritizes Parent1 over Parent2. If you want to call the greet method from Base, you can do so explicitly:

print(Base.greet(child_instance))  # Output: 'Hello from Base!'

This example illustrates how Python's MRO handles potential conflicts in multiple inheritance.

Example 3: MRO and the Super Function

Python provides a built-in function called super() that can be used to access methods from parent classes in a controlled manner. For example:

class Base:
    def greet(self):
        return 'Hello from Base!'

class Parent1(Base):
    def greet(self):
        return super().greet() + ' and Parent1!'

class Parent2(Base):
    def greet(self):
        return super().greet() + ' and Parent2!'

class Child(Parent1, Parent2):
    def greet(self):
        return super().greet() + ' and Child!'

child_instance = Child()
print(child_instance.greet())  # Output: 'Hello from Base! and Parent1! and Child!'

In this scenario, using super() allows Child to call the greet method in a way that respects the MRO, ensuring that all parent classes are accounted for. This method enhances the flexibility and maintainability of your code, making it easier to manage complex hierarchies.

Keywords

Pythonmultiple inheritanceobject-oriented programmingclass relationshipsMethod Resolution Order

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions