Python program to illustrate Single Inheritance (2)

Here, we will see a Python program to see how single inheritance works?
Submitted by Shivang Yadav, on February 19, 2021

Single Inheritance:

When a class inherits the properties of another class, it is known as single inheritance.

Python | Single Inheritance

The class that inherits the properties of another class is known as derived Class.

The class whose properties are inherited is known as base Class.

Classes used in the program:

  • Class : Employee
    • Method : getEmployeeInfo() : Gets input of the employee information from the user.
    • Method : printEmployeeInfo() : prints the information of the employee.
    • Method : getSalary() : return the salary of the employee.
  • Class Perks :
    • Method: getPerks() : calculates all perks of the employee.
    • Method : putPerks() : prints all perks and employee details of the employee.

Program to illustrate single inheritance in Python

class Employee:
 def getEmployeeInfo(self):
   self.__id=input("Enter Employee Id:")
   self.__name=input("Enter Name:")
   self.__salary=int(input("Enter Employee Salary:"))

 def printEmployeeInfo(self):
   print("ID : ", self.__id," , name : ", self.__name, ", Basic Salary : ", self.__salary)

 def getSalary(self):
     return(self.__salary)

class Perks(Employee):
    def getPerks(self):
        self.getEmployeeInfo()
        sal=self.getSalary()
        self.__da=sal*35/100
        self.__hra = sal * 17 / 100
    
    def printPerks(self):
        self.printEmployeeInfo()
        print("Total Salary ", (self.getSalary() + self.__da + self.__hra ) )

S=Perks()
S.getPerks()

print("Employee information ")
S.printPerks()

Output:

Enter Employee Id:0012
Enter Name:shivang 
Enter Employee Salary:50000
Employee information 
ID :  0012  , name :  shivang  , Basic Salary :  50000
Total Salary  76000.0

Python class & object programs »



Related Programs



Comments and Discussions!

Load comments ↻





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