Inheritance in Python:
In object oriented programming, inheritance is the process of inheriting the properties of one class in another class. The original class is called parent class or base class. The newly created class is called child class or subclass.
Inheritance is an important concept of object oriented programming. We can use inheritance to avoid rewriting the same code. We can create a new class by reusing the code of another class.
The child class will have access to all the members, methods and other properties of the parent class. It can also change the methods of the parent class.
Inheritance is useful while creating new classes with the existing properties of other classes. We can also create child class of child classes. These classes will also inherit the properties of the parent class.
Syntax of inheritance:
Before move to detail, let me show you the syntax:
class ParentClass:
# body
class ChildClass(ParentClass):
#body
As you can see here, the parent class needs to be passed in the parenthesis of the child class.
How to access the parent class properties from child class:
We can use the super() method to access any parent class methods. Let’s take a look at the below example:
class ParentClass:
a = 10
b = 20
def get_sum(self):
print('Inside parent class')
return self.a + self.b
class ChildClass(ParentClass):
def get_sum(self):
print('Inside child class')
print(f'a = {self.a}, b = {self.b}')
return super().get_sum()
c = ChildClass()
print(c.get_sum())
- ParentClass and ChildClass are parent and child classes.
- a and b are two variables defined in the parent class.
- Both classes has a method called get_sum.
- The get_sum of the parent class returns the sum of a and b.
- The get_sum of the child class returns the result of parent class’s get_sum. It uses super() method to access the get_sum method of the parent class. It also prints the values of a and b. Note that even though a and b variables are not initialized in the child class, it can access these.
- c is an object of the child class. The last line calls the get_sum() method of the child class.
It will print:
Inside child class
a = 10, b = 20
Inside parent class
30
Different types of inheritance in Python:
We can categorize inheritance in 5 types in Python based upon how the parent and child classes are linked. Following are these types:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
1.Single inheritance:
In single inheritance, one child class is linked with one parent class. This is the simplest form of inheritance.
class ParentClass:
def hello_parent(self):
print('Inside ParentClass.')
class ChildClass(ParentClass):
def hello_child(self):
print('Inside ChildClass.')
child = ChildClass()
child.hello_child()
child.hello_parent()
This is the simplest form of inheritance. One parent class is linked to one child class. One object of the child class, child is created and it calls the methods hello_child and hello_parent. One method is defined in the child class and another is in the parent class.
It will print the below output:
Inside ChildClass.
Inside ParentClass.
2.Multiple inheritance:
Python supports multiple inheritance, i.e. one child class can inherit from multiple parent classes. The below example will use one child class with two parent classes:
class FirstParentClass:
def hello_first(self):
print('Inside FirstParentClass.')
class SecondParentClass:
def hello_second(self):
print('Inside SecondParentClass.')
class ChildClass(FirstParentClass, SecondParentClass):
def hello_child(self):
print('Inside ChildClass.')
child = ChildClass()
child.hello_child()
child.hello_first()
child.hello_second()
We have to add all parent classes separated by comma in the child class parenthesis. It will print:
Inside ChildClass.
Inside FirstParentClass.
Inside SecondParentClass.
3.Multilevel inheritance:
In multilevel inheritance, classes are linked like a chain. For example, if we have four classes class1, class2, class3 and class4, these classes can be linked with multilevel inheritance:
class1 -> class2 -> class3 -> class4
i.e. class2 is the child class of class1, class3 is the child class of class2 and class4 is the child class of class3.
The below example uses multilevel inheritance:
class ParentClass:
def hello_parent(self):
print('Inside ParentClass.')
class FirstChildClass:
def hello_first(self):
print('Inside FirstChildClass.')
class SecondChildClass(FirstChildClass, ParentClass):
def hello_second(self):
print('Inside SecondChildClass.')
child = SecondChildClass()
child.hello_parent()
child.hello_first()
child.hello_second()
Output:
Inside ParentClass.
Inside FirstChildClass.
Inside SecondChildClass.
4.Hierarchical inheritance:
In Hierarchical inheritance, multiple child classes are linked to a single parent class. Or, multiple classes can have the same parent class.
class ParentClass:
def hello_parent(self):
print('Inside ParentClass.')
class FirstChildClass(ParentClass):
def hello_first(self):
print('Inside FirstChildClass.')
class SecondChildClass(ParentClass):
def hello_second(self):
print('Inside SecondChildClass.')
first_child = FirstChildClass()
second_child = SecondChildClass()
first_child.hello_parent()
first_child.hello_first()
second_child.hello_parent()
second_child.hello_second()
In this example, ParentClass is the parent class of both FirstChildClass and SecondChildClass. We are creating objects of both FirstChildClass and SecondChildClass classes. It will print:
Inside ParentClass.
Inside FirstChildClass.
Inside ParentClass.
Inside SecondChildClass.
5. Hybrid Inheritance:
In hybrid inheritance, multiple types of inheritance exists. For example:
class ParentClass:
def hello_parent(self):
print('Inside ParentClass.')
class FirstChildClass(ParentClass):
def hello_first(self):
print('Inside FirstChildClass.')
class SecondChildClass(ParentClass):
def hello_second(self):
print('Inside SecondChildClass.')
class ThirdChildClass(SecondChildClass):
def hello_third(self):
print('Inside ThirdChildClass.')
This example shows a hybrid inheritance. The classes ParentClass, FirstChildClass and SecondChildClass are using Hierarchical inheritance and ThirdChildClass and SecondChildClass are using single inheritance. We can mix more than one type of inheritance in between the same set of classes.