Python Program to Implement Multiple Inheritance

Introduction

Multiple inheritance is a feature of object-oriented programming where a class can inherit attributes and methods from more than one parent class. This allows a child class to combine the functionality of multiple parent classes. However, multiple inheritance can also lead to complexity, especially when different parent classes have methods with the same name.

This tutorial will guide you through creating a Python program that demonstrates multiple inheritance by defining multiple parent classes and a child class that inherits from all of them.

Example:

  • Parent Classes: Person, Employee
  • Child Class: Manager
  • Attributes: name, age (from Person), employee_id, department (from Employee)
  • Methods: display_person_info() (from Person), display_employee_info() (from Employee), display_manager_info() (from Manager)
  • Program Output:
    Name: Alice, Age: 35
    Employee ID: E12345, Department: HR
    

Problem Statement

Create a Python program that:

  • Defines a class named Person with attributes name and age, and a method display_person_info.
  • Defines a class named Employee with attributes employee_id and department, and a method display_employee_info.
  • Defines a class named Manager that inherits from both Person and Employee, and has an additional method display_manager_info.
  • Creates an object of the Manager class and calls the methods to display the details.

Solution Steps

  1. Define the Person Class: Use the class keyword to define a class named Person.
  2. Define the Employee Class: Use the class keyword to define a class named Employee.
  3. Define the Manager Class: Use the class keyword to define a class named Manager that inherits from both Person and Employee.
  4. Initialize Attributes in the Manager Class: Use the super() function to call the initialization methods of both Person and Employee.
  5. Add Methods to Manager: Define a method named display_manager_info that combines the information from Person and Employee.
  6. Create an Object: Instantiate an object of the Manager class.
  7. Call the Methods: Call the methods on the Manager object to display the details.

Python Program

# Python Program to Implement Multiple Inheritance
# Author: https://www.rameshfadatare.com/

# Step 1: Define the Person Class
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def display_person_info(self):
        print(f"Name: {self.name}, Age: {self.age}")

# Step 2: Define the Employee Class
class Employee:
    def __init__(self, employee_id, department):
        self.employee_id = employee_id
        self.department = department
    
    def display_employee_info(self):
        print(f"Employee ID: {self.employee_id}, Department: {self.department}")

# Step 3: Define the Manager Class with Multiple Inheritance
class Manager(Person, Employee):
    def __init__(self, name, age, employee_id, department):
        Person.__init__(self, name, age)
        Employee.__init__(self, employee_id, department)
    
    # Step 5: Add a Method to Display Manager's Information
    def display_manager_info(self):
        self.display_person_info()
        self.display_employee_info()

# Step 6: Create an Object of the Manager Class
manager = Manager("Alice", 35, "E12345", "HR")

# Step 7: Call the Methods to Display Manager's Details
manager.display_manager_info()

Explanation

Step 1: Define the Person Class

  • The Person class is defined with the attributes name and age. It also includes a method display_person_info that prints the person’s details.

Step 2: Define the Employee Class

  • The Employee class is defined with the attributes employee_id and department. It also includes a method display_employee_info that prints the employee’s details.

Step 3: Define the Manager Class with Multiple Inheritance

  • The Manager class is defined, inheriting from both Person and Employee. This is indicated by the syntax class Manager(Person, Employee):.

Step 4: Initialize Attributes in the Manager Class

  • The __init__ method of the Manager class calls the __init__ methods of both Person and Employee classes to initialize their respective attributes.

Step 5: Add a Method to Manager

  • The display_manager_info method is defined in the Manager class. This method calls display_person_info and display_employee_info to display the combined information from both parent classes.

Step 6: Create an Object

  • An object manager is created by calling the Manager class with specific arguments for name, age, employee_id, and department.

Step 7: Call the Methods

  • The display_manager_info method is called on the manager object to display the manager’s details, demonstrating multiple inheritance.

Output Example

Example Output:

Name: Alice, Age: 35
Employee ID: E12345, Department: HR

Additional Examples

Example 1: Multiple Inheritance with Additional Methods

# Define a class Developer that also inherits from Person and Employee
class Developer(Person, Employee):
    def __init__(self, name, age, employee_id, department, programming_language):
        Person.__init__(self, name, age)
        Employee.__init__(self, employee_id, department)
        self.programming_language = programming_language
    
    def display_developer_info(self):
        self.display_person_info()
        self.display_employee_info()
        print(f"Programming Language: {self.programming_language}")

# Create an object of the Developer class
developer = Developer("Bob", 28, "D54321", "IT", "Python")
developer.display_developer_info()

Output:

Name: Bob, Age: 28
Employee ID: D54321, Department: IT
Programming Language: Python

Example 2: Method Resolution Order (MRO)

# Demonstrating MRO in multiple inheritance
class A:
    def show(self):
        print("Class A")

class B(A):
    def show(self):
        print("Class B")

class C(A):
    def show(self):
        print("Class C")

class D(B, C):
    pass

# Create an object of class D
obj = D()
obj.show()  # Output will depend on the method resolution order

Output:

Class B
  • In this example, the method show is inherited from B first due to the method resolution order (MRO) in Python, which follows the "C3 linearization" algorithm.

Example 3: Multiple Inheritance with Overlapping Methods

# Define classes with overlapping methods
class X:
    def method(self):
        print("Method from class X")

class Y:
    def method(self):
        print("Method from class Y")

class Z(X, Y):
    pass

# Create an object of class Z
z = Z()
z.method()  # Output will depend on the MRO

Output:

Method from class X
  • Since X is listed first in the inheritance, its method is called.

Conclusion

This Python program demonstrates how to implement multiple inheritance, a powerful feature that allows a class to inherit from more than one parent class. While multiple inheritance can be useful, it should be used carefully due to the complexity it can introduce, especially with method resolution order and potential name conflicts. Understanding multiple inheritance is essential for advanced object-oriented programming in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top