Inheritance In Python | FACE Prep

Under the concept of OOPs, the next important component is Inheritance in Python. Inheritance allows one class to inherit all the properties of another class. The class which inherits all the properties is called a child or derived class. And the class from which the properties are being derived is called parent or base class.

Inheriting all the properties refers to the ability of a child class to access all variables and methods defined inside a parent class. Before understanding them in detail, let us see how inheritance makes programming much easier.

Why do we need Inheritance?

  • Inheritance provides code reusability as we can use an existing parent class to create a new child class instead of creating a child class from scratch.
  • Inheritance is transitive in nature, i.e., if class B inherits class A, then all the subclasses of B can also inherit class A. 

To implement inheritance in the Python program, we need to create a parent class and a child class. So, let us see how to create these two classes.

Creating Parent Class

The steps to create a parent class are similar to that of creating a normal class as explained in the article on classes and objects. Consider a parent class named ‘Car’ which has a variable ‘length’ and a method ‘drive’ as shown below.

#parent class
class Car:
    length = '470cm'
    def drive(self):
        print("I can drive at a speed of 60km/hr")

Creating Child Class

A child class is created by using the keyword ‘class’ followed by the child class name and a colon. It accepts parent class as an argument as shown in the below syntax.

Syntax for creating a child class

class ChildClass(ParentClass):
    Body of ChildClass

Example
class RaceCar(Car):
    pass

Next, let us see how to access the method of the parent class using an object created for the child class.

#main() method
maruti = RaceCar()
maruti.drive()

Here, the object ‘maruti’ is created for the child class ‘RaceCar’ and using which the parent class method ‘drive’ is accessed. Since ‘RaceCar’ inherits the data members of ‘Car’, it can access the method defined inside ‘Car’. 

We can also define a method inside the child class as shown in the below example.

#parent class
class Car:
    length = '470cm'
    def drive(self):  #parent class method
        print("I can drive at a speed of 60km/hr")
 
#child class
class RaceCar(Car):
    def rashdrive(self):  #child class method
        print("I can drive at a speed of 40km/hr")

#main() method
maruti = RaceCar()
maruti.drive()  #accessing the parent class method
maruti.rashdrive()  #accessing the child class method
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 40km/hr

So, a child class can access data members defined inside it as well as inside the parent class as shown in the below image.

inheritance first

Python has different types of inheritance. Let us discuss about them with examples.

Types Of Inheritance In Python

Python supports 5 types of inheritance namely,

  1. Single
  2. Multiple
  3. Multilevel
  4. Hierarchical
  5. Hybrid

a) Single Inheritance

In single inheritance, a child class inherits all the properties of a parent class.

inheritance second

The above-given code is an example of a single inheritance.

b) Multiple Inheritance in Python 

In Multiple inheritance, a child class inherits the properties of more than one parent class.

inheritance third

We need to mention the names of the parent class as comma-separated in the child class. In the below example, the child class ‘RaceCar’ inherits all the properties of ‘Car1’ and ‘Car2’.

#the first parent class
class Car1:
    length = '470cm'
    def drive1(self):
        print("I can drive at a speed of 60km/hr")

#the second parent class

class Car2:
    length = '400cm'
    def drive2(self):
        print("I can drive at a speed of 70km/hr")

#child class
class RaceCar(Car1, Car2):
    def rashdrive(self):
        print("I can drive at a speed of 40km/hr")

#main() method
maruti = RaceCar()
maruti.drive1()
maruti.drive2()
maruti.rashdrive()
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 70km/hr
I can drive at a speed of 40km/hr

c) Multilevel Inheritance in Python

Using this type of inheritance, we can achieve a grandchild relationship. The grandchild is a class which inherits all the properties of the existing child class.

inheritance fourth

In the below example, ‘RaceCar2’ is the grandchild that inherits all the properties of the child class ‘RaceCar1’.

#parent class
class Car:
    length = '470cm'
    def drive(self):
        print("I can drive at a speed of 60km/hr")

#child class
class RaceCar1(Car):
    def rashdrive1(self):
        print("I can drive at a speed of 40km/hr")

#grandchild class
class RaceCar2(RaceCar1):
    def rashdrive2(self):
        print("I can drive at a speed of 30km/hr")

#main() method
maruti = RaceCar2()
maruti.drive()
maruti.rashdrive1()
maruti.rashdrive2()
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 40km/hr
I can drive at a speed of 30km/hr

d) Hierarchical Inheritance in Python

In this type of inheritance, more than one child classes inherit all the properties of the same parent class.

inheritance fifth

In the below example, ‘RaceCar1’ and ‘RaceCar2’ are the two child classes that inherit all the properties of the parent class ‘Car’.

#parent class
class Car:
    length = '470cm'
    def drive(self):
        print("I can drive at a speed of 60km/hr")

#child class 1
class RaceCar1(Car):
    def rashdrive1(self):
        print("I can drive at a speed of 40km/hr")

#child class 2

class RaceCar2(Car):
    def rashdrive2(self):
        print("I can drive at a speed of 30km/hr")

#main() method
maruti = RaceCar2()
maruti.drive()
maruti.rashdrive2()
Output:
I can drive at a speed of 60km/hr
I can drive at a speed of 30km/hr

In the above example, if we try to access the method ‘rashdrive1’ using the object ‘maruti’, the Python interpreter throws an error. 

e) Hybrid Inheritance in Python

Hybrid inheritance is a combination of multiple and multilevel inheritance. More than one child class inherits a parent class. Those two child class, in turn, acts as the parent class for a grandchild class as shown in the below image.

inheritance sixth

Inheritance in Python FAQs

Inheritance in Python allows one class to inherit all the properties of another class. Inheriting all the properties refers to the ability of a child class to access all the variables and methods defined inside a parent class.

Python supports five different types of inheritance namely Single, Multiple, Multilevel, Hierarchical and Hybrid.