Home » Python » Python Inheritance Tutorial

About Yatin Batra

Yatin Batra
An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).

Python Inheritance Tutorial

Hello in this tutorial, we will understand inheritance in python programming.

1. Introduction

Inheritance is an important concept in any programming language. It provides code reusability as we can use the existing classes to create new classes instead of creating it from scratch. In this, the child class acquires the properties of the parent class and can access all the data members and functions defined in the parent class. A child class can also provide its specific implementation to the functions of the parent class. In python different type of inheritance are supported i.e. –

  • Single inheritance – When a child class only inherits a single parent class
  • Multiple inheritance – When a child class inherits from multiple parent classes
  • Multilevel inheritance – When a child class becomes a parent class for another child class. There is no limit on the levels up to which the multiple inheritances can be achieved

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python Inheritance Tutorial

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 Single inheritance

Let us understand this with the help of a code snippet.

Single inheritance

# base class
class Vehicle:
    def start(self):
        print('Start vehicle')
# child class Audi inherits the base class Vehicle
class Audi(Vehicle):
    def start_audi(self):
        print('Start audi')
# creating object
ob = Audi()
ob.start_audi()
ob.start()

If everything goes well the following output will be shown in the IDE console.

Console Output

Start audi
Start vehicle

2.2 Multilevel inheritance

Let us understand this with the help of a code snippet.

Multilevel inheritance

# base class
class Vehicle:
    def start(self):
        print('Start vehicle')
# child class Audi inherits the base class Vehicle
class Audi(Vehicle):
    def start_audi(self):
        print('Start audi')
# child class AudiChild inherits another child class Audi
class AudiChild(Audi):
    def start_engine(self):
        print('Starting audi engine')
# creating object
ob = AudiChild()
ob.start_engine()
ob.start_audi()
ob.start()

If everything goes well the following output will be shown in the IDE console.

Console Output

Starting audi engine
Start audi
Start vehicle

2.3 Multiple inheritance

Let us understand this with the help of a code snippet.

Multiple inheritance

# base class
class Vehicle:
    def start(self):
        print('Start vehicle')
# base class
class Audi:
    def start_audi(self):
        print('Start audi')
# child class Hybrid inherits from Vehicle and Audi
class Hybrid(Vehicle, Audi):
    def start_hybrid(self):
        print('Start hybrid')
# creating object
ob = Hybrid()
ob.start_hybrid()
ob.start_audi()
ob.start()

If everything goes well the following output will be shown in the IDE console.

Console Output

Start hybrid
Start audi
Start vehicle

2.4 Method overriding

In child class we can provide the specific implementation of a method defined in the base class and this concept is known as method overriding. We use this where a different definition of a parent class method is needed in the child class. Let us understand this with the help of a code snippet.

Method overriding

# base class
class Vehicle:
    def start(self):
        print('Start vehicle')
# child class Audi inherits the base class Vehicle
class Audi(Vehicle):
    def start(self):
        print('Start audi')
# creating object
ob = Audi()
ob.start()

If everything goes well the following output will be shown in the IDE console.

Console Output

Start audi

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • An introduction to inheritance in python programming
  • Sample programs to understand the different inheritance levels in python programming

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial of inheritance in python programming.

Download
You can download the full source code of this example here: Python Inheritance Tutorial
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments