Inheritance in Python
The process of inheriting the properties of the parent class into a child class is called
inheritance. The existing class is called a base class or parent class and the new class is
called a subclass or child class or derived class.
Benefits of inheritance are:
➢ It represents real-world relationships well.
➢ It provides the reusability of a code. We don’t have to write the same code again and
again. Also, it allows us to add more features to a class without modifying it.
➢ It is transitive in nature, which means that if class B inherits from another class A, then
all the subclasses of B would automatically inherit from class A.
➢ Inheritance offers a simple, understandable model structure.
Python Inheritance Syntax
The syntax of simple inheritance in Python is as follows:
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
Types Of Inheritance
In Python, based upon the number of child and parent classes involved, there are five
types of inheritance. The type of inheritance are listed below:
1. Single inheritance
2. Multiple Inheritance
3. Multilevel inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
In single inheritance, a child class inherits from a single-
parent class. Here is one child class and one parent class.
EX:
class parent: # parent class
def func1(self):
print("Hello Parent")
class child(parent):
# child class
def func2(self): # we include the parent class
print("Hello Child") # as an argument in the child class
# Driver Code
test = child() # object created
test.func1() # parent method called via child object
test.func2() # child method called
Multiple Inheritance
In multiple inheritance, a single child class is inherited from two or more parent classes.
It means the child class has access to all the parent classes' methods and attributes.
Example:
Basic implementation of multiple inheritance
class parent1: # first parent class
def func1(self):
print("Hello Parent1")
class parent2: # second parent class
def func2(self):
print("Hello Parent2")
class parent3: # third parent class
def func2(self): # the function name is same as parent2
print("Hello Parent3")
class child(parent1, parent2, parent3): # child class
def func3(self): # we include the parent classes
print("Hello Child") # as an argument comma separated
# Driver Code
test = child() # object created
test.func1() # parent1 method called via child
test.func2() # parent2 method called via child instead of parent3
test.func3() # child method called
Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or
derived class. Suppose three classes A, B, C. A is the
superclass, B is the child class of A, C is the child class of B.
In other words, we can say a chain of classes is called
multilevel inheritance.
Example:
class grandparent: # first level
def func1(self):
print("Hello Grandparent")
class parent(grandparent): # second level
def func2(self):
print("Hello Parent")
class child(parent): # third level
def func3(self):
print("Hello Child")
# Driver Code
test = child() # object created
test.func1() # 3rd level calls 1st level
test.func2() # 3rd level calls 2nd level
test.func3()
Hierarchical Inheritance
In Hierarchical inheritance, more than one child
class is derived from a single parent class. In
other words, we can say one parent class and
multiple child classes.
Example:
class parent: # parent class
def func1(self):
print("Hello Parent")
class child1(parent): # first child class
def func2(self):
print("Hello Child1")
class child2(parent): # second child class
def func3(self):
print("Hello Child2")
# Driver Code
test1 = child1() # objects created
test2 = child2()
test1.func1() # child1 calling parent method
test1.func2() # child1 calling its own method
test2.func1() # child2 calling parent method
test2.func3()
Output:
Hello Parent
Hello Child1
Hello Parent
Hello Child2
Hybrid Inheritance in Python
Hybrid Inheritance is the mixture of two or more
different types of inheritance. Here we can have
many relationships between parent and child
classes with multiple levels.
Example:
# python 3 syntax
# hybrid inheritance example
class parent1: # first parent class
def func1(self):
print("Hello Parent")
class parent2: # second parent class
def func2(self):
print("Hello Parent")
class child1(parent1): # first child class
def func3(self):
print("Hello Child1")
class child2(child1, parent2): # second child class
def func4(self):
print("Hello Child2")
# Driver Code
test1 = child1() # object created
test2 = child2()
test1.func1() # child1 calling parent1 method
test1.func3() # child1 calling its own method
test2.func1() # child2 calling parent1 method
test2.func2() # child2 calling parent2 method
test2.func3() # child2 calling child1 method
test2.func4() # child2 calling its own method
Polymorphism
Poly means many. Morphs means forms.
Polymorphism means 'Many Forms'.
Eg1: Student is best example of polymorphism. In front of his parents he will have one
type of behaviour and with friends another type of behaviour. Same person but different
behaviours at different places, which is nothing but polymorphism.
Eg2: + operator acts as concatenation and arithmetic addition
Eg3: * operator acts as multiplication and repetition operator
Eg4: The Same method with different implementations in Parent class and child
classes.(overriding)
Related to polymorphism the following 2 topics are important
1. Overloading
1. Operator Overloading
2. Method Overloading (not supported in python)
3. Constructor Overloading ((not supported in python)
2. Overriding
1. Method overriding
2. constructor overriding
1. Operator Overloading:
We can use the same operator for multiple purposes, which is nothing but operator
overloading.
Python supports operator overloading.
Eg1: + operator can be used for Arithmetic addition and String concatenation
print(10+20)#30
print('nsv'+'dc')#nsvdc
Eg2: * operator can be used for multiplication and string repetition purposes.
print(10*20)#200
print('nsv'*3)#nsvnsvnsv
Demo program to use + operator for our class objects:
class Book:
def __init__(self,pages):
[Link]=pages
b1=Book(100)
b2=Book(200)
print(b1+b2)
2. Method overriding:
Whatever members available in the parent class are by default available to the child class
through inheritance. If the child class not satisfied with parent class implementation then
child class is allowed to redefine that method in the child class based on its requirement.
This concept is called overriding.
Overriding concept applicable for both methods and constructors.
Demo Program for Method overriding:
class P:
def property(self):
print('Gold+Land+Cash+Power')
def marry(self):
print(‘BlackGirl’)
class C(P):
def marry(self):
print('WhiteGirl’)
c=C()
[Link]()
[Link]()
Output:
Gold+Land+Cash+Power
WhiteGirl