Python Inheritance and OOPs Fundamentals with Examples

In this tutorial, we’ll discuss Python Inheritance, the core object-oriented programming concept. You’ll learn – what is Inheritance, how to extend classes, how to override class methods and attributes, what is the purpose of the super() function.

In the next tutorial, we’ll cover the concept of multiple-inheritance in Python.

Inheritance lets us define a class that derives all the functionality from the parent class while giving us scope to add more.

We recommend reading the Python class tutorial first if you’ve not already gone through it.

Python Tutorials

Python Inheritance – Introduction

Inheritance is the core feature of object-oriented programming which extends the functionality of an existing class by adding new features. You may compare it with real-life situations when a child inherits the property of his parents in addition to adding his own. He may even derive the surname (the second name) from his parents.

By using the inheritance feature, we can have a new blueprint with old attributes but without making any changes to the original one. We refer the new class as the derived or child class whereas the old one becomes the base or parent class.

Python Inheritance & OOP Concepts

Python Inheritance & OOP Concepts

How to use Inheritance in Python?

You can introduce inheritance by using the following syntax.

class ParentClass:
  Parent class attributes
  Parent class methods
class ChildClass(ParentClass):
  Child class attributes
  Child class methods

Inheritance automatically brings reusability to your code as the derived class has got everything from the base class.

Python Inheritance – Example

To understand the application of inheritance, let’s consider the following example.

We have a base class Taxi, and it has a subclass (child) Vehicle.

class Taxi:

    def __init__(self, model, capacity, variant):
        self.__model = model      # __model is private to Taxi class
        self.__capacity = capacity
        self.__variant = variant

    def getModel(self):          # getmodel() is accessible outside the class
        return self.__model

    def getCapacity(self):         # getCapacity() function is accessible to class Vehicle
        return self.__capacity

    def setCapacity(self, color):  # setCapacity() is accessible outside the class
        self.__capacity = capacity

    def getVariant(self):         # getVariant() function is accessible to class Vehicle
        return self.__variant

    def setVariant(self, variant):  # setVariant() is accessible outside the class
        self.__variant = variant

class Vehicle(Taxi):

    def __init__(self, model, capacity, variant, color):
        # call parent constructor to set model and color  
        super().__init__(model, capacity, variant)
        self.__color = color

    def vehicleInfo(self):
        return self.getModel() + " " + self.getVariant() + " in " + self.__color + " with " + self.getCapacity() + " seats"

# In method getInfo we can call getmodel(), getCapacity() as they are 
# accessible in the child class through inheritance

v1 = Vehicle("i20 Active", "4", "SX", "Bronze")
print(v1.vehicleInfo())
print(v1.getModel()) # Vehicle has no method getModel() but it is accessible via Vehicle class

v2 = Vehicle("Fortuner", "7", "MT2755", "White")
print(v2.vehicleInfo())
print(v2.getModel()) # Vehicle has no method getModel() but it is accessible via Vehicle class

Please note that we have not specified the getName() method in the Vehicle class, but we can access it. It is because the Vehicle class inherits it from the Taxi class.

The output of the above example is as follows.

# output
i20 Active SX in Bronze with 4 seats
i20 Active
Fortuner MT2755 in White with 7 seats
Fortuner

To bring more clarity, you can refer the below Python inheritance’s UML diagram of the example mentioned above.

Python Inheritance Model

The super() method

The super() method allows us to access the inherited methods that cascade to a class object.

In the earlier example, we’ve used the super() method in the constructor of the child class <Vehicle>. It is invoking the function of the base class <Taxi>.

Now, you might be thinking – how does the super() method work?

Just assume, if you have to invoke a method in the base class, i.e., vehicleInfo() defined in the child class, then you can use the following code.

super().vehicleInfo()

Likewise, you can invoke the base class constructor from the sub (child) class __init__using the below code.

super().__init__()

Quick wrap up – Python Inheritance and OOP

In this tutorial, we covered “Python Inheritance” which is essential in object-oriented programming. Hence, it is utmost necessary that you are aware of how the inheritance works in Python.

Now, if you’ve learned something from this class, then care to share it with your colleagues. Also, connect to our social media (Facebook/Twitter) accounts to receive timely updates.

Best,

TechBeamers