Python Tutorials: Inheritance In Python

Python Tutorials: An Introduction to Classes and Inheritance (in Python)
Written by Paayi Tech |01-Aug-2020 | 0 Comments | 351 Views

Inheritance is another major aspect of object-oriented programming. By the inheritance, we can pass on the functionalities of the derived class to the base class. A class that inherits from the superclass is called a base class or often child class. By the inheritance method, we can share the information with other classes without making it purely public.

Here it is to be noted that the inherited class can only access the protected data members. So, we don't have to make all things 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 a 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 the following class, there are three classes employee and programmer. The programmer is inherited by the employee and use the functionality of the 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, the last name of the employee, and the pay. There is another parameter of the appraisal. The employee class has two methods one is getting the full name, and the other is increased salary. Then we defined another class called a programmer. The programmer class inherits from the employee and take an extra parameter of language. We initialize the super constructor.

Then we create the instance pass the parameter, and by the instance of the programmer, we can access the functionalities of employees to manipulate the values.

The output of the above-given code is following;



Ash Ketchum

Salary after increment 46800


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

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.