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(fromPerson),employee_id,department(fromEmployee) - Methods:
display_person_info()(fromPerson),display_employee_info()(fromEmployee),display_manager_info()(fromManager) - Program Output:
Name: Alice, Age: 35 Employee ID: E12345, Department: HR
Problem Statement
Create a Python program that:
- Defines a class named
Personwith attributesnameandage, and a methoddisplay_person_info. - Defines a class named
Employeewith attributesemployee_idanddepartment, and a methoddisplay_employee_info. - Defines a class named
Managerthat inherits from bothPersonandEmployee, and has an additional methoddisplay_manager_info. - Creates an object of the
Managerclass and calls the methods to display the details.
Solution Steps
- Define the
PersonClass: Use theclasskeyword to define a class namedPerson. - Define the
EmployeeClass: Use theclasskeyword to define a class namedEmployee. - Define the
ManagerClass: Use theclasskeyword to define a class namedManagerthat inherits from bothPersonandEmployee. - Initialize Attributes in the
ManagerClass: Use thesuper()function to call the initialization methods of bothPersonandEmployee. - Add Methods to
Manager: Define a method nameddisplay_manager_infothat combines the information fromPersonandEmployee. - Create an Object: Instantiate an object of the
Managerclass. - Call the Methods: Call the methods on the
Managerobject 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
Personclass is defined with the attributesnameandage. It also includes a methoddisplay_person_infothat prints the person’s details.
Step 2: Define the Employee Class
- The
Employeeclass is defined with the attributesemployee_idanddepartment. It also includes a methoddisplay_employee_infothat prints the employee’s details.
Step 3: Define the Manager Class with Multiple Inheritance
- The
Managerclass is defined, inheriting from bothPersonandEmployee. This is indicated by the syntaxclass Manager(Person, Employee):.
Step 4: Initialize Attributes in the Manager Class
- The
__init__method of theManagerclass calls the__init__methods of bothPersonandEmployeeclasses to initialize their respective attributes.
Step 5: Add a Method to Manager
- The
display_manager_infomethod is defined in theManagerclass. This method callsdisplay_person_infoanddisplay_employee_infoto display the combined information from both parent classes.
Step 6: Create an Object
- An object
manageris created by calling theManagerclass with specific arguments forname,age,employee_id, anddepartment.
Step 7: Call the Methods
- The
display_manager_infomethod is called on themanagerobject 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
showis inherited fromBfirst 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
Xis listed first in the inheritance, itsmethodis 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.