Python Inheritance Explained
Python Inheritance Explained
Python - Inheritance
Inheritance is one of the most important features of object-oriented programming languages like
Python. It is used to inherit the properties and behaviours of one class to another. The class that
inherits another class is called a child class and the class that gets inherited is called a base class or
parent class.
If you have to design a new class whose most of the attributes are already well defined in an existing
class, then why redefine them? Inheritance allows capabilities of existing class to be reused and if
required extended to design a new class.
Inheritance comes into picture when a new class possesses 'IS A' relationship with an existing class.
For example, Car IS a vehicle, Bus IS a vehicle, Bike IS also a vehicle. Here, Vehicle is the parent class,
whereas car, bus and bike are the child classes.
Page 2 of 13
Powered by:
The class whose attributes and methods are inherited is called as parent class. It is defined just like
other classes i.e. using the class keyword.
Syntax
class ParentClassName:
{class body}
Classes that inherit from base classes are declared similarly to their parent class, however, we need to
provide the name of parent classes within the parentheses.
Syntax
Page 3 of 13
Types of Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Page 4 of 13
This is the simplest form of inheritance where a child class inherits attributes and methods from only
one parent class.
Example
Open Compiler
# parent class
class Parent:
def parentMethod(self):
print ("Calling parent method")
# child class
class Child(Parent):
def childMethod(self):
print ("Calling child method")
# instance of child
c = Child()
# calling method of child class
Page 5 of 13
c.childMethod()
# calling method of parent class
c.parentMethod()
Multiple inheritance in Python allows you to construct a class based on more than one parent classes.
The Child class thus inherits the attributes and method from all parents. The child can override
methods inherited from any parent.
Syntax
class parent1:
#statements
class parent2:
#statements
Example
Page 6 of 13
Python's standard library has a built-in divmod() function that returns a two-item tuple. First number is
the division of two arguments, the second is the mod value of the two operands.
This example tries to emulate the divmod() function. We define two classes division and modulus, and
then have a div_mod class that inherits them.
class division:
def __init__(self, a,b):
self.n=a
self.d=b
def divide(self):
return self.n/self.d
class modulus:
def __init__(self, a,b):
self.n=a
self.d=b
def mod_divide(self):
return self.n%self.d
class div_mod(division,modulus):
def __init__(self, a,b):
self.n=a
self.d=b
def div_and_mod(self):
divval=division.divide(self)
modval=modulus.mod_divide(self)
return (divval, modval)
The child class has a new method div_and_mod() which internally calls the divide() and mod_divide()
methods from its inherited classes to return the division and mod values.
x=div_mod(10,3)
print ("division:",x.divide())
print ("mod_division:",x.mod_divide())
print ("divmod:",x.div_and_mod())
Output
division: 3.3333333333333335
mod_division: 1
Page 7 of 13
divmod: (3.3333333333333335, 1)
The term method resolution order is related to multiple inheritance in Python. In Python,
inheritance may be spread over more than one levels. Let us say A is the parent of B, and B the parent
for C. The class C can override the inherited method or its object may invoke it as defined in its parent.
So, how does Python find the appropriate method to call.
Each Python has a mro() method that returns the hierarchical order that Python uses to resolve the
method to be called. The resolution order is from bottom of inheritance order to top.
In our previous example, the div_mod class inherits division and modulus classes. So, the mro method
returns the order as follows −
In multilevel inheritance, a class is derived from another derived class. There exists multiple layers of
inheritance. We can imagine it as a grandparent-parent-child relationship.
Example
Open Compiler
# parent class
class Universe:
def universeMethod(self):
print ("I am in the Universe")
# child class
class Earth(Universe):
def earthMethod(self):
print ("I am on Earth")
# creating instance
person = India()
# method calls
person.universeMethod()
person.earthMethod()
person.indianMethod()
When we execute the above code, it will produce the following result −
I am in the Universe
I am on Earth
I am in India
This type of inheritance contains multiple derived classes that are inherited from a single base class.
This is similar to the hierarchy within an organization.
Example
The following example illustrates hierarchical inheritance. Here, we have defined two child classes of
Manager class.
Open Compiler
# parent class
class Manager:
def managerMethod(self):
print ("I am the Manager")
# child class
class Employee1(Manager):
def employee1Method(self):
print ("I am Employee one")
# creating instances
emp1 = Employee1()
emp2 = Employee2()
# method calls
emp1.managerMethod()
emp1.employee1Method()
emp2.managerMethod()
emp2.employee2Method()
On executing the above program, you will get the following output −
I am the Manager
I am Employee one
I am the Manager
I am Employee two
Combination of two or more types of inheritance is called as Hybrid Inheritance. For instance, it could
be a mix of single and multiple inheritance.
Example
In this example, we have combined single and multiple inheritance to form a hybrid inheritance of
classes.
Open Compiler
# parent class
class CEO:
def ceoMethod(self):
print ("I am the CEO")
class Manager(CEO):
def managerMethod(self):
print ("I am the Manager")
class Employee1(Manager):
def employee1Method(self):
print ("I am Employee one")
Page 10 of 13
# creating instances
emp = Employee2()
# method calls
emp.managerMethod()
emp.ceoMethod()
emp.employee2Method()
I am the Manager
I am the CEO
I am Employee two
In Python, super() function allows you to access methods and attributes of the parent class from
within a child class.
Example
In the following example, we create a parent class and access its constructor from a subclass using
the super() function.
Open Compiler
# parent class
class ParentDemo:
def __init__(self, msg):
self.message = msg
def showMessage(self):
print(self.message)
# child class
class ChildDemo(ParentDemo):
Page 11 of 13
# creating instance
obj = ChildDemo("Welcome to Tutorialspoint!!")
obj.showMessage()
Welcome to Tutorialspoint!!
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Git Tutorial
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
DevOps Certification
Online Go Compiler
Online C Compiler
Online C# Compiler
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical
and non-technical subjects.