Inheritance In Python

An Introduction to Classes and Inheritance (in Python)
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 240 Views

Inheritance is another major aspect in object-oriented programming. By the inheritance we can pass on the functionalities of derive class to base class. A class that inherits from the super class is called base class or often child class. By the inheritance method we can share the information to other class without making it purely public. Here it is to be noted that inherit class can only access the protected data members. So, we don’t have to make all thing public and can hide the information following is the example of inheritance in python by which the concept of private protected and public data members can also be cleared.


class MyClass(object):
    def __init__(self):
        self.public = "public"
        self._protected = "protected"
        self.__private = "private"
class InheritClass(MyClass):
    def __init__(self):
        print(f"Public data member: {MyClass().public}")
        print(f"Protected data member: {MyClass()._protected}")
        print(f"Private data member: {MyClass().__private}")


We don’t have to initialize the base class we can access only by derived class. The output of the following code is as follows:



Public data member: public

Protected data member: protected


Traceback (most recent call last):

  File "/home/lalatoofani/", line 15, in


  File "/home/lalatoofani/", line 13, in __init__

    print(f"Private data member: {MyClass().__private}")

AttributeError: 'MyClass' object has no attribute '_InheritClass__private'

 Here we can see that private members cannot be accessed.



Now we will see a detailed example of inheritance. In following class there are three classes employee and programmer. The programmer is inherited by the employee and use the functionality of employee class:

class Employee:
    def __init__(self,fname,lname,pay):
        self.fname = fname
        self.lname = lname = pay
        self.appraisal = 1.04
    def fullname(self):
        return f"{self.fname} {self.lname}"
    def increaseSalary(self): = int(float(*self.appraisal)
class Programmer(Employee):
    def __init__(self,fname,lname,pay,language):
        self.language = language
prog1 = Programmer('Ash','Ketchum','45000','python')
print(f"Salary after increment {}")


We defined a class Employee in which it takes the first name of the employee, last name of the employee and the pay. There is another parameter of appraisal. The employee class have two methods one is getting the full name and other is increase salary. Than we defined another class called programmer. The programmer class inherits from the Employee and take extra parameter of language. We initialize the super constructor.

Than we create the instance pass the parameter and by the instance of programmer we can access the functionalities of Employee to manipulate the values.

The output of the above given code is following;



Ash Ketchum

Salary after increment 46800



Similarly, we can do multiple inheritance and execute more than on derived class at a time. Following is the example of multiple inheritance:

class First(object):
    def __init__(self):
        print('First Class is Executed')
class Second(object):
    def __init__(self):
        print('Second class is executed')
class Third(Second, First):
    def __init__(self):
        print('Third Class is executed')




First Class is Executed

Second class is executed

Third Class is executed




Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.