Home »
Python »
Python programs
Multilevel Inheritance Example in Python
Here, we are going to learn about the Multilevel Inheritance in Python and demonstrating the Multilevel Inheritance using a Python program.
Submitted by Shivang Yadav, on February 16, 2021
Problem Statement: We will see a program to illustrate the working of multilevel inheritance in Python.
Problem Description: The program will calculate the final salary of an employee from basic salary using multilevel inheritance.
Multilevel Inheritance in Python:
Multilevel inheritance in programming (general in object-oriented program). When a base class is derived by a derived class which is derived by another class, then it is called multilevel inheritance.
The below diagram will make things clearer,
All Class used in the program and the methods:
-
Class: Employee
- Method: getEmployee() -> gets user input from id, name and salary.
- Method: printEmployeeDetails() -> prints employee detail
- Method: getSalary() -> return the salary of the employee.
-
Class: Perks
- Method: calcPerks() -> calculates all perks using the salaries.
- Method: putPerks() -> prints all perks.
- Method: totalPerks() -> return the sum of all perks.
-
Class : NetSalary:
- Method: getTotal() -> calculates net Salary using perks and base salary.
- Method: showTotal() -> prints total salary.
Program to illustrate multilevel inheritance in Python
class Employee:
def getEmployee(self):
self.__id = input("Enter Employee Id: ")
self.__name = input("Enter Name: ")
self.__salary = int(input("Enter Employees Basic Salary: "))
def printEmployeeDetails(self):
print(self.__id,self.__name,self.__salary)
def getSalary(self):
return(self.__salary)
class Perks(Employee):
def calcPerks(self):
self.getEmployee()
sal=self.getSalary()
self.__da=sal*35/100
self.__hra = sal * 17 / 100
self.__pf=sal*12/100
def putPerks(self):
self.printEmployeeDetails()
print(self.__da,self.__hra,self.__pf)
def totalPerks(self):
t=self.__da+self.__hra-self.__pf
return (t)
class NetSalary(Perks):
def getTotal(self):
self.calcPerks()
self.__ns=self.getSalary()+self.totalPerks()
def showTotal(self):
self.putPerks()
print("Total Salary:",self.__ns)
empSalary = NetSalary()
empSalary.getTotal()
empSalary.showTotal()
Output:
Enter Employee Id: 001
Enter Name: John
Enter Employees Basic Salary: 25000
001 John 25000
8750.0 4250.0 3000.0
Total Salary: 35000.0
Python class & object programs »