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.
You can download the full source code of this example here: Python Inheritance Tutorial