Example of multiple inheritance in Python

Here, we are going to implement a python program to demonstrate an example of multiple inheritance.
Submitted by Pankaj Singh, on June 25, 2019

Multiple inheritance

When we have one child class and more than one parent classes then it is called multiple inheritance i.e. when a child class inherits from more than one parent class.

In this program, we have two parent classes Personel and Educational and one child class named Student and implementing multiple inheritance.

Python code to demonstrate example of multiple inheritance

# Python code to demonstrate example of 
# multiple inheritance 

class Personel:
    def __init__(self):
        self.__id=0
        self.__name=""
        self.__gender=""
    def setPersonel(self):
        self.__id=int(input("Enter Id: "))
        self.__name = input("Enter Name: ")
        self.__gender = input("Enter Gender: ")
    def showPersonel(self):
        print("Id: ",self.__id)
        print("Name: ",self.__name)
        print("Gender: ",self.__gender)

class Educational:
    def __init__(self):
        self.__stream=""
        self.__year=""
    def setEducational(self):
        self.__stream=input("Enter Stream: ")
        self.__year = input("Enter Year: ")
    def showEducational(self):
        print("Stream: ",self.__stream)
        print("Year: ",self.__year)

class Student(Personel,Educational):
    def __init__(self):
        self.__address = ""
        self.__contact = ""
    def setStudent(self):
        self.setPersonel()
        self.__address = input("Enter Address: ")
        self.__contact = input("Enter Contact: ")
        self.setEducational()

    def showStudent(self):
        self.showPersonel()
        print("Address: ",self.__address)
        print("Contact: ",self.__contact)
        self.showEducational()

def main():
    s=Student()
    s.setStudent()
    s.showStudent()
if __name__=="__main__":main()

Output

Enter Id: 101
Enter Name: Prem Sharma  
Enter Gender: Male   
Enter Address: Nehru Place, New Delhi
Enter Contact: 0123456789
Enter Stream: Computer Science   
Enter Year: 2010 
Id:  101 
Name:  Prem Sharma   
Gender:  Male
Address: Nehru Place, New Delhi 
Contact: 0123456789 
Stream:  Computer Science
Year:  2010

Python class & object programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.