Summary: Inheritance is one of the properties of Python (object-oriented programming language). Inheritance in Python enables code reusablity and as a result, the complexity of the program decreases. Let’s learn about inheritance with examples.

Introduction to Python Inheritance

Inheritance in python is a way through which we can use the property of one class into another class. This ensures the reusability of code and hence reduces the complexity of the program.

The class which inherits another class is called derived or sub class and the class which has been inherited is known as the parent or super class.

The following block diagram illustrates the flow of properties in Python Inheritance:

Block Diagram of Inheritance in Python


Similar to other OOP languages, Inheritance in Python works by extending one class to another.

To inherit or extend a class, we provide the base class name inside the parenthesis of the derived class.

It is important to note that we should only inherit from another class if there is a ‘IS A‘ relationship.

For example, Dog is an Animal (dog has the properties of an animal).

Let’s see this in action with python program.

Recommended: Class and objects in Python.

Example 1: Dog is an Animal


I am an Animal
I am a Dog
I can eat
I can Bark


We have created an ‘Animal’ class and inherited it in the dog class.

Using the super() method we have called the Animal class __init__() method by Dog class __init__() method.

Super() method returns the reference of Base class, hence allowing us to use base class reference into any derived class.

In the driver code, we have created an object of the Dog class and invoked all the methods of both classes. As you can see, that without any error we are able to call eat() method of the Animal class using Dog class object, which proves that we have successfully inherited the Animal class into Dog class i.e. implemented inheritance.

Let’s see one more example of inheritance.

Example 2: Saving Account is a Bank Account


200 deposit Successful
300 withdraw Successful
Current Balance: 700
Interest: 241.50

Hope now you will feel a little bit confident in writing inheritance related programs in python. If you have any doubts then comment below.

This Post Has One Comment

  1. taduri

    I liked your simple straight forward examples 🙂

Leave a Reply

2 × five =