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
Person
with attributesname
andage
, and a methoddisplay_person_info
. - Defines a class named
Employee
with attributesemployee_id
anddepartment
, and a methoddisplay_employee_info
. - Defines a class named
Manager
that inherits from bothPerson
andEmployee
, and has an additional methoddisplay_manager_info
. - Creates an object of the
Manager
class and calls the methods to display the details.
Solution Steps
- Define the
Person
Class: Use theclass
keyword to define a class namedPerson
. - Define the
Employee
Class: Use theclass
keyword to define a class namedEmployee
. - Define the
Manager
Class: Use theclass
keyword to define a class namedManager
that inherits from bothPerson
andEmployee
. - Initialize Attributes in the
Manager
Class: Use thesuper()
function to call the initialization methods of bothPerson
andEmployee
. - Add Methods to
Manager
: Define a method nameddisplay_manager_info
that combines the information fromPerson
andEmployee
. - Create an Object: Instantiate an object of the
Manager
class. - 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 attributesname
andage
. It also includes a methoddisplay_person_info
that prints the person’s details.
Step 2: Define the Employee Class
- The
Employee
class is defined with the attributesemployee_id
anddepartment
. It also includes a methoddisplay_employee_info
that prints the employee’s details.
Step 3: Define the Manager Class with Multiple Inheritance
- The
Manager
class is defined, inheriting from bothPerson
andEmployee
. This is indicated by the syntaxclass Manager(Person, Employee):
.
Step 4: Initialize Attributes in the Manager Class
- The
__init__
method of theManager
class calls the__init__
methods of bothPerson
andEmployee
classes to initialize their respective attributes.
Step 5: Add a Method to Manager
- The
display_manager_info
method is defined in theManager
class. This method callsdisplay_person_info
anddisplay_employee_info
to display the combined information from both parent classes.
Step 6: Create an Object
- An object
manager
is created by calling theManager
class with specific arguments forname
,age
,employee_id
, anddepartment
.
Step 7: Call the Methods
- The
display_manager_info
method is called on themanager
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 fromB
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, itsmethod
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.