Example of multilevel inheritance in Python

Multilevel inheritance

At the point when we have a youngster class and grandkid class – it is called staggered legacy for example at the point when a class acquires on below average and inferior further acquires on another class.

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

Python code to demonstrate an 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

Leave a Comment

error: Alert: Content is protected!!