Inheritance in python

Inheritance:

  • It is the process of joining two or more classes and we can access the values from both classes.

Types of Inheritance:

1) Single inheritance

2) Multilevel inheritance

3) Multiple inheritances

Benefits of inheritance:

  • Software re-usability
  • Information hiding
  • Code sharing

Syntax:

  1. class class_name1:

        #function definition

    class class_name2(class_name1):

        #function definition

    object_name=class_name(parameter_list)#Object creation

    object_name.function_name(parameter_list)#function calling

Example:

  1. #inheritance with constructor

    class employee:

        def __init__(self,id,name,depart,sal):

            self.id=id

            self.name=name

            self.depart=depart

            self.sal=sal

        def display(self):

            print("\tEmployee details")

            print("ID :",self.id)

            print("Name:",self.name)

            print("Department :",self.depart)

            print("Basic Salary : ",self.sal)

    class salary_cal(employee):

        def sal_cal(self)://access sal from base class

            if self.sal>=15000:

                self.sal+=5000

                print("Increased salary:5000")

                print("Net salary:",self.sal)

            elif self.sal>=1000 and self.sal<15000:

                self.sal+=3000

                print("Increased salary:3000")

                print("Net salary:",self.sal)

            else:

                self.sal+=1000

                print("Increased salary:1000")

                print("Net salary:",self.sal)

    id=input("Enter employee id:")

    name=input("Enter employee name:")

    depart=input("Enter employee department:")

    sal=int(input("Enter basic salary:"))

    #object creation

    emp=salary_cal(id,name,depart,sal)

    #emp=employee(id,name,depart,sal)

    emp.display()

    emp.sal_cal()

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me