Example of multilevel inheritance in Python

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

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.

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

Python class & object programs »



Related Programs



Comments and Discussions!

Load comments ↻





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