Q:

Example of multiple inheritance in Python

belongs to collection: Python class & object programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Example of multilevel inheritance in Python... >>
<< Example of inheritance with two child (derived) cl...