Dremendo Tag Line

Inheritance in Python Programming

OOPs in Python

In this lesson, we will understand what is Inheritance and how to use it in class object programming.

What is Inheritance in Python?

In python, when we create a new class by utilizing the code of an existing class, then this process is known as Inheritance.

In inheritance, we don't copy the code from an existing class to a new class. Instead, we include the existing class in a new class when we create the new class.

Let's take an example to clear the concept of inheritance. Suppose we have a class called Information with two instance variables: roll, name, and two instance methods as inputdata() and displaydata(). See the code given below.

video-poster

Code for Information Class

class Information:

    # Creating instance variables
    def __init__(self):
        self.roll = 0
        self.name = ""

    # Creating the instance method inputinfo
    def inputinfo(self):
        self.roll = int(input('Roll No: '))
        self.name = input('Name: ')

    # Creating the instance method displayinfo
    def displayinfo(self):
        print('Roll No: %d' %(self.roll))
        print('Name: %s' %(self.name))

We want to create a new class called Result that will store roll, name, marks of three subjects (English, Maths, Computer), total marks, and percentage obtained.

As the class Information already exists that can store roll and name, we will create our new class Result by inheriting all the codes from the Information class without copying and pasting the codes from the Information class into the Result class. See the complete example given below.

Example of Inheritance

class Information:

    # Creating instance variables
    def __init__(self):
        self.roll = 0
        self.name = ""

    # Creating the instance method inputinfo
    def inputinfo(self):
        self.roll = int(input('Enter Roll No: '))
        self.name = input('Enter Name: ')

    # Creating the instance method displayinfo
    def displayinfo(self):
        print('Roll No: %d' %(self.roll))
        print('Name: %s' %(self.name))


# Creating the class Result by inheriting the class Information
class Result(Information):

    # Creating the instance variables of Result class
    def __init__(self):
        self.eng = 0
        self.math = 0
        self.comp = 0
        self.total = 0
        self.per = 0

    # Creating the instance method inputdata
    def inputdata(self):
        self.inputinfo()   # Calling the inputinfo() of the base class Information
        self.eng = int(input('Enter English: '))
        self.math = int(input('Enter Math: '))
        self.comp = int(input('Enter Computer: '))
        self.total = self.eng + self.math + self.comp
        self.per = (self.total/300.0)*100

    # Creating the instance method displaydata
    def displaydata(self):
        self.displayinfo()   # Calling the displayinfo() of the base class Information
        print('English: %d' %(self.eng))
        print('Math: %d' %(self.math))
        print('Computer: %d' %(self.comp))
        print('Total Marks: %d' %(self.total))
        print('Percentage: %.2f' %(self.per))


# Creating object x of class Result
x = Result()
x.inputdata()
x.displaydata()

Output

Enter Roll No: 1
Enter Name: Thomas
Enter English: 72
Enter Math: 86
Enter Computer: 90
Roll No: 1
Name: Thomas
English: 72
Math: 86
Computer: 90
Total Marks: 248
Percentage: 82.67

In the above program, We can see that we have created a new class Result by inheriting the class Information.

The class Result is created by inheriting the class Information. So we can call the Information class as Base class or Parent class and the Result class as Derived class or Subclass or Child class.

When a child class inherits the parent class, all the instance variables and instance methods of the parent class are available to the child class. We can access the instance variables and instance methods of the parent class from within the child class using the word self.

For example, see on lines 40 and 49 of the above program how we have accessed the instance methods inputinfo() and displayinfo() of the Information class using self.

Accessing the Base Class Instance Variables and Instance Methods using the Child Class's Object

To access the base class instance variable and instance methods using the child class's object, we have to call the constructor of the base class inside the child class's constructor. After that, the object of the child class can access the instance variable and methods of the parent class.

We can call the base class constructor inside the child's class in two ways.

  • Using the name of the base or parent class.
  • Using the super() function.

Example using base class name

class Information:

    # Creating instance variables
    def __init__(self):
        self.roll = 0
        self.name = "--"

    # Creating the instance method inputinfo
    def inputinfo(self):
        self.roll = int(input('Enter Roll No: '))
        self.name = input('Enter Name: ')

    # Creating the instance method displayinfo
    def displayinfo(self):
        print('Roll No: %d' %(self.roll))
        print('Name: %s' %(self.name))


# Creating the class Result by inheriting the class Information
class Result(Information):

    # Creating the instance variables of Result class
    def __init__(self):
        # Calling the constructor of the base class using its class name
        Information.__init__(self)
        self.eng = 0
        self.math = 0


# Creating object x of child class Result
x = Result()

# Accessing the instance variables and instance methods
# of the base class using the child's class object
print(x.roll)
print(x.name)
x.inputinfo()
x.displayinfo()

Output

0
--
Enter Roll No: 1
Enter Name: Thomas
Roll No: 1
Name: Thomas

Example using super() function

class Information:

    # Creating instance variables
    def __init__(self):
        self.roll = 0
        self.name = "--"

    # Creating the instance method inputinfo
    def inputinfo(self):
        self.roll = int(input('Enter Roll No: '))
        self.name = input('Enter Name: ')

    # Creating the instance method displayinfo
    def displayinfo(self):
        print('Roll No: %d' %(self.roll))
        print('Name: %s' %(self.name))


# Creating the class Result by inheriting the class Information
class Result(Information):

    # Creating the instance variables of Result class
    def __init__(self):
        # Calling the constructor of the base class using super() function
        super().__init__()
        self.eng = 0
        self.math = 0


# Creating object x of child class Result
x = Result()

# Accessing the instance variables and instance methods
# of the base class using the child's class object
print(x.roll)
print(x.name)
x.inputinfo()
x.displayinfo()

Output

0
--
Enter Roll No: 1
Enter Name: Peter
Roll No: 1
Name: Peter

Use of super() Function in Inheritance

The super() function is a built-in function in python that refers to the parent class in inheritance. With the help of the super() function, we can call the constructor and instance methods of the parent class from within the child class without explicitly using the parent class name, thus making the code more maintainable.

Note: We can't access the instance variables of the parent class by super() function from within the child class. To access the instance variables of the parent class inside the child class, we have to use the word self.

Example

class Employee:

    # Creating instance variables
    def __init__(self):
        self.id = 0
        self.name = "--"

    # Creating the instance method inputinfo
    def inputinfo(self):
        self.id = int(input('Enter ID: '))
        self.name = input('Enter Name: ')

    # Creating the instance method displayinfo
    def displayinfo(self):
        print('ID: %d' %(self.id))
        print('Name: %s' %(self.name))


# Creating the class Salary by inheriting the class Employee
class Salary(Employee):

    # Creating the instance variables of Salary class
    def __init__(self):
        # Calling the constructor of the base class using super() function
        super().__init__()
        self.basic = 0
        self.da = 0

    # Creating the instance method inputinfo
    def inputinfo(self):
        super().inputinfo()     # calling the parent class instance method
        self.basic = int(input('Enter Basic Pay: '))
        self.da = int(input('Enter DA: '))

    # Creating the instance method displayinfo
    def displayinfo(self):
        super().displayinfo()   # calling the parent class instance method
        print('Basic Pay: %d' %(self.basic))
        print('DA: %d' %(self.da))


# Creating object x of child class Salary
x = Salary()
x.inputinfo()
x.displayinfo()

Output

Enter ID: 101
Enter Name: William
Enter Basic Pay: 12000
Enter DA: 7000
ID: 101
Name: William
Basic Pay: 12000
DA: 7000

The above example shows that the name of instance methods are the same in both classes. If we call the instance method of the parent class with the word self, then instead of calling the parent's class instance method, it will call the child class instance method, thus creating an infinite recursion, and the program will end up with an error. In this case, we use the super() function to call the instance methods of the parent class from inside the child class.