Q:

Example of multilevel inheritance in Python

belongs to collection: Python class & object programs

0

Multilevel inheritance

When we have a child class and grandchild class – it is called multilevel inheritance i.e. when a class inherits on second class and second class further inherits on another class.

In this program, we have a parent class named Details1 which is inheriting on class Details2 and class Details2 further inheriting on the class Details3.

All Answers

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

Python code to demonstrate example of multilevel inheritance

# Python code to demonstrate example of 
# multilevel inheritance 

class Details1:
    def __init__(self):
        self.__id=0
    def setId(self):
        self.__id=int(input("Enter Id: "))
    def showId(self):
        print("Id: ",self.__id)

class Details2(Details1):
    def __init__(self):
        self.__name=""
    def setName(self):
        self.setId()
        self.__name=input("Enter Name: ")
    def showName(self):
        self.showId()
        print("Name: ",self.__name)

class Details3(Details2):
    def __init__(self):
        self.__gender=""
    def setGender(self):
        self.setName()
        self.__gender=input("Enter Gender: ")
    def showGender(self):
        self.showName()
        print("Gender: ",self.__gender)


class Employee(Details3):
    def __init__(self):
        self.__desig=""
        self.__dept=""
    def setEmployee(self):
        self.setGender()
        self.__desig=input("Enter Designation: ")
        self.__dept= input("Enter Department: ")
    def showEmployee(self):
        self.showGender()
        print("Designation: ",self.__desig)
        print("Department: ",self.__dept)

def main():
    e = Employee()
    e.setEmployee()
    e.showEmployee()


if __name__=="__main__":main()

Output

Enter Id: 101  
Enter Name: Prem Sharma 
Enter Gender: Male
Enter Designation: Technical writer 
Enter Department: Computer science  
Id:  101 
Name:  Prem Sharma
Gender:  Male  
Designation:  Technical writer
Department:  Computer science

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 (2)... >>
<< Example of multiple inheritance in Python...