Write a program to multiple inheritance in Python


This Python program demonstrates the concept of multiple inheritance by creating a child class, Student, that inherits from two parent classes, PersonalInfo and AcademicInfo. The program collects personal and academic information from the user and then displays the combined student details, including both personal and academic information. Here's an explanation of the program:

  • class PersonalInfo: This is the parent class for personal information. It contains an __init__ constructor that initializes attributes like idn, name, gender, address, and contact. It also includes a display_personal_info method to display the personal information.
  • class AcademicInfo: This is the parent class for academic information. It contains an __init__ constructor that initializes attributes like stream and year. It also includes a display_academic_info method to display the academic information.
  • class Student(PersonalInfo, AcademicInfo) : This is the child class that inherits from both PersonalInfo and AcademicInfo. It overrides the __init__ constructor to call the constructors of both parent classes. It also includes a display_student_details method to display both personal and academic information.
  • In the main part of the program, the user is prompted to enter personal and academic information using the input function.
  • An instance of the Student class, named student, is created by providing the collected information as arguments to the constructor.
  • Finally, the display_student_details method is called on the student object to display all the student's details.

When you run this program, it collects and displays a student's personal and academic information by inheriting and using the methods and attributes from both the PersonalInfo and AcademicInfo parent classes. This demonstrates multiple inheritance, where a child class can inherit from multiple parent classes to reuse their attributes and methods.


Source Code

class PersonalInfo:	# parent classe
    def __init__(self, idn, name, gender, address, contact):
        self.idn = idn
        self.name = name
        self.gender = gender
        self.address = address
        self.contact = contact
 
    def display_personal_info(self):
        print("Id :   ", self.idn)
        print("Name : ", self.name)
        print("Gender : ", self.gender)
        print("Address : ", self.address)
        print("Contact : ", self.contact)
 
class AcademicInfo:	# parent classe
    def __init__(self, stream, year):
        self.stream = stream
        self.year = year
 
    def display_academic_info(self):
        print("Stream : ", self.stream)
        print("Year :   ", self.year)
 
class Student(PersonalInfo, AcademicInfo):	#child class inheriting from both parent classes
    def __init__(self, idn, name, gender, address, contact, stream, year):
        # Call constructors of parent classes
        PersonalInfo.__init__(self, idn, name, gender, address, contact)
        AcademicInfo.__init__(self, stream, year)
 
    def display_student_details(self):
        self.display_personal_info()
        self.display_academic_info()
 
idn = input("Enter the ID : ")
name = input("Enter the Name : ")
gender = input("Enter the Gender : ")
address = input("Enter the Address : ")
contact = input("Enter the Contact : ")
stream = input("Enter the Stream : ")
year = input("Enter the Year : ")
 
# Create a Student instance
student = Student(idn, name, gender, address, contact, stream, year)
 
# Display student details
student.display_student_details()
 

Output

Enter the ID : 103
Enter the Name : Mithra
Enter the Gender : Female
Enter the Address : 123 - Main Street
Enter the Contact : 9043017689
Enter the Stream : Computer Science
Enter the Year : 2023
Id :    103
Name :  Mithra
Gender :  Female
Address :  123 - Main Street
Contact :  9043017689
Stream :  Computer Science
Year :    2023

Example Programs