Table of Contents
In this tutorial I will take you through Inheritance concepts in Python with Best Working Examples. If we talk about a real life example, Inheritance is when a child inherits some of the properties from their parents. It can be the eye color, skin color or any other gene characteristics. In a similar way if we look into the programming aspects inheritance is when a class inherits some of the properties from another class. A class which inherits the properties is known as Child Class or Derived Class and the class from which the properties get inherited is known as Base Class or Parent Class. There are multiple types of Inheritance in Python.
Types of Inheritance in Python
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Advantages of Using Inheritance in Python
- It is mostly used for Code Reusability so that you don’t have to write same code again
and again. - It saves time and efforts.
- It led to the creation of Class Libraries.
- It proves the reliability of Base Class.
- It represents real world relationships.
- It allows to put all the standard methods and codes in base class so that any derived class can use them.
- It makes the code more readable and easy to understand.
Inheritance Concepts in Python with Best Working Examples
Also Read: How to List all the Installed Modules in Linux{2 Easy Methods}
1. Single Inheritance
In python when a derived class inherits only from a single base class, it is called Single inheritance
. In below inheritance concept example you can see that Child class
inherits child()
method from Mother Class
. So that when you create object of Child Class
and call obj.child()
method, it will display "Adam is Child"
.
Program Example
class Mother():
def child(self):
print("Adam is child")
class Child(Mother):
def child1(self):
print("Anny is grandchild")
obj=Child()
obj.child()
obj.child1()
Program Output
[root@localhost ~]# python example.py Adam is child Anny is grandchild
2. Multiple Inheritance
When a class is derived from one or more parent class, it is called Multiple Inheritance
. In the below python program you can see that Grandchild class
inheriting mother()
method from Mother Class
and child()
method from Child Class
so that when we are creating object of Grandchild Class
it is able to invoke both inherited mother()
and child()
methods.
Program Example
class Mother():
def mother(self):
print("I am mother")
class Child():
def child(self):
print("I am child")
class Grandchild(Mother, Child):
def grandchild(self):
print("I am grand child")
obj=Grandchild()
obj.mother()
obj.child()
obj.grandchild()
Program Output
[root@localhost ~]# python example.py I am mother I am child I am grand child
3. Multilevel Inheritance
When one class inherits from another class which again inherits from another class, it is called Multilevel Inheritance
. In the below shown example, Grandchild Class
inheriting from Child Class
and Child Class
inheriting from Mother Class
, so when you create object of Grandchild Class
you will be able to call all the inherited methods.
Program Example
class Mother():
def mother(self):
print("I am mother")
class Child(Mother):
def child(self):
print("I am child")
class Grandchild(Child):
def grandchild(self):
print("I am grand child")
obj=Grandchild()
obj.mother()
obj.child()
obj.grandchild()
Program Output
[root@localhost ~]# python example.py I am mother I am child I am grand child
4. Hierarchical Inheritance
When more than one derived class are created from same parent class, it is called Hierarchical Inheritance
. Below example shows Child1
and Child2
Class inheriting from Mother Class
. Both of the Derived classes are able to create objects and invoke the base class mother()
method.
Program Example
class Mother:
def mother(self):
print("I am Mother")
class Child1(Mother):
def child1(self):
print("I am First child")
class Child2(Mother):
def child2(self):
print("I am Second child")
ob = Child1()
ob1 = Child2()
ob.mother()
ob.child1()
Program Output
[root@localhost ~]# python example.py I am Mother I am First child
5. Hybrid Inheritance
It is a combination of more than one type of inheritance is called Hybrid Inheritance
. Below example shows the implementation of all types of python inheritance concepts. You can see object of Child1
and Child2
Class invokes single inheritance and call derived mother()
method. Similarly object of Child3
class invokes multiple
, multilevel
and hierarchical
derived methods. Hence it shows the perfect implementation of Hybrid Inheritance
.
Program Example
class Mother:
def mother(self):
print("I am Mother")
class Child1(Mother):
def child1(self):
print("I am First child")
class Child2(Mother):
def child2(self):
print("I am Second child")
class Child3(Child1,Child2):
def child3(self):
print("I am child from First and Second Child")
ob = Child1()
ob1 = Child2()
ob2 = Child3()
ob.mother()
ob.child1()
print("\n")
ob2.child1()
ob2.child2()
ob2.child3()
Program Output
[root@localhost ~]# python example.py I am Mother I am First child I am First child I am Second child I am child from First and Second Child
Popular Recommendations:-
Solved: ModuleNotFoundError No module named “numpy” in Python3
Python3: ModuleNotFoundError No module named “prettytable” in Linux
Solved: ModuleNotFoundError No module named “requests” in Python3
Python Inheritance Concepts Official Documentation
How to Check Stateful and Stateless Pods in Kubernetes Cluster
3 Easy Ways to Check/Find OpenSUSE Linux Version
6 Easy Steps to Setup and Manage Log rotation Using logrotate in Linux