Q:

Example of single inheritance in Python

belongs to collection: Python class & object programs

0

In this program, we have a parent class named Details and child class named Employee, we are inheriting the class Details on the class Employee. And, finally creating an object of Employee class and by calling the method setEmployee() – we are assigning the values to the class variables and printing the values using showEmployee() function.

All Answers

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

Python code to demonstrate example of single inheritance

# Python code to demonstrate example of 
# single inheritance

class Details:
    def __init__(self):
        self.__id="<No Id>"
        self.__name="<No Name>"
        self.__gender="<No Gender>"
    def setData(self,id,name,gender):
        self.__id=id
        self.__name=name
        self.__gender=gender
    def showData(self):
        print("Id\t\t:",self.__id)
        print("Name\t\t:", self.__name)
        print("Gender\t\t:", self.__gender)

class Employee(Details): #Inheritance
    def __init__(self):
        self.__company="<No Company>"
        self.__dept="<No Dept>"
    def setEmployee(self,id,name,gender,comp,dept):
        self.setData(id,name,gender)
        self.__company=comp
        self.__dept=dept
    def showEmployee(self):
        self.showData()
        print("Company\t\t:", self.__company)
        print("Department\t:", self.__dept)


def main():
    e=Employee()
    e.setEmployee(101,"Prem Sharma","Male","New Delhi",110065)
    e.showEmployee()

if __name__=="__main__":
    main()

Output

Id              : 101
Name            : Prem Sharma
Gender          : Male
Company         : New Delhi
Department      : 110065

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
Python program to illustrate Single Inheritance (2... >>
<< Python | Create Employee Class with Constructor an...