Professional Documents
Culture Documents
Unit-4 Inheritance
Unit-4 Inheritance
Unit-4 Inheritance
ipynb - Colaboratory
Code reusability- we do not have to write the same code again and again, we can just inherit the
properties we need in a child class.
It represents a real world relationship between parent class and child class.
It is transitive in nature. If a child class inherits properties from a parent class, then all other sub-
classes of the child class will also inherit the properties of the parent class.
Example:
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 1/12
12/4/2020 Inheritance.ipynb - Colaboratory
First, a (closed oriented) polygon is formed by a sequence of points in the XY plane called vertices
which are connected with a polyline into a closed loop. So, a triangle is a special case of a polygon
with three vertices, and a quadrilateral is a special case of a polygon with four vertices. Therefore,
the classes Triangle and Quadrilateral should naturally be descendants of the class Polygon.
Further, a rectangle is a special case of a quadrilateral whose adjacent edges have right angles
between them. Hence the class Rectangle should be inherited from the class Quadrilateral. And
last, a square is a special case of a rectangle whose edges are equally long. Thus the class Square
should be derived from the class Rectangle.
While de ning the child class, the name of the parent class is put in the parentheses in front of it,
indicating the relation between the two. Instance attributes and methods de ned in the parent class
will be inherited by the object of the child class.
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 2/12
12/4/2020 Inheritance.ipynb - Colaboratory
Simple examples:
class Parent():
def first(self):
print('first function')
class Child(Parent):
def second(self):
print('second function')
ob = Child()
ob.first()
ob.second()
first function
second function
#defining constructor
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge
def showAge(self):
print(self.age)
def getId(self):
print(self.studentId )
#returns the value of student id
#end of subclass definition
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 3/12
12/4/2020 Inheritance.ipynb - Colaboratory
Person age
23
student details
Max
102
22
Another example:
class Polygon:
self.n = no_of_sides
def inputSides(self):
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)
def findArea(self):
a b c = self sides
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 4/12
12/4/2020 Inheritance.ipynb - Colaboratory
a, b, c = self.sides
s = (a + b + c) / 2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-5124ee1956fd> in <module>()
18 print("Side",i+1,"is",self.sides[i])
19
---> 20 class Triangle(Polygon):
21
22 def __init__(self):
<ipython-input-15-5124ee1956fd> in Triangle()
30 # calculate the semi-perimeter
31
---> 32 s = (a + b + c) / 2
33
34 area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
Python also has a super() function that will make the child class inherit all the methods and
properties from its parent:
class quadriLateral:
def __init__(self, a, b, c, d):
self.side1=a
self.side2=b
self.side3=c
self.side4=d
def perimeter(self):
p=self.side1 + self.side2 + self.side3 + self.side4
print("perimeter=",p)
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 5/12
12/4/2020 Inheritance.ipynb - Colaboratory
class rectangle(quadriLateral):
def __init__(self, a, b):
super().__init__(a, b, a, b)
def area(self):
a = self.side1 * self.side2
print("area of rectangle=", a)
r1=rectangle(10, 20)
r1.perimeter()
r1.area()
perimeter= 60
area of rectangle= 200
Overriding in Python Methods of the parent class are available for use in the inherited class.
However, if needed, we can modify the functionality of any base class method. For that purpose, the
inherited class contains a new de nition of a method (with the same name and the signature
already present in the base class). Naturally, the object of a new class will have access to both
methods, but the one from its own class will have precedence when invoked. This is called method
overriding.
class parent:
def hi():
print("greeting from parent")
class child(parent):
def hi():
print("greeting from child")
c=child
p=parent
c.hi()
1. Single inheritance: When a child class inherits from only one parent class, it is called single
inheritance. We saw an example above.
2. Multiple inheritance: When a child class inherits from multiple parent classes, it is called
multiple inheritance.
4. Hierarchical inheritance More than one derived classes are created from a single base.
5. Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a
blend of more than one type of inheritance.
class Base2():
def __init__(self):
self.str2 = "Inheritance"
print("Base2")
def printStrs(self):
print(self.str1, self.str2)
ob = Derived()
ob.printStrs()
Base1
Base2
Derived
python Inheritance
class Base():
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 7/12
12/4/2020 Inheritance.ipynb - Colaboratory
return self.name
# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age
# To get name
def getAge(self):
return self.age
# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address
# To get address
def getAddress(self):
return self.address
# Driver code
g = GrandChild("Anu", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())
Anu 23 Noida
#Hierarchical Inheritance
class Parent:
def func1(self):
print("this is function 1")
class Child1(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child1()
ob1 = Child2()
ob.func1()
ob1.func1()
this is function 1
this is function 1
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 8/12
12/4/2020 Inheritance.ipynb - Colaboratory
Generalization may be de ned as the technique of extracting the essential characteristics (these
include attributes, properties and methods) from two or more subclasses and then combining them
inside a generalized base class (also called a superclass). On the contrary, specialization is the
reverse of generalization -- it's used to represent "type-of" relationship by creating subclasses from
existing base classes.
Pure Function
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 9/12
12/4/2020 Inheritance.ipynb - Colaboratory
Examples
The below function is pure. It has no side effects and always returns the same output for the same
input.
def add_1(x):
return x + 1
If you'd like functions to be pure, then do not change the value of the input or any data that exists
outside the function's scope.
This makes the function we write much easier to test. As it does not change the state of any
variable, we are guaranteed to get the same output every time we run the function with the same
input.
The original list of numbers are unchanged, and we don't reference any other variables outside of
the function, so it is pure.
def multiply_2_pure(numbers):
new_numbers = []
for n in numbers:
new_numbers.append(n * 2)
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 10/12
12/4/2020 Inheritance.ipynb - Colaboratory
return new_numbers
Impure functions
There are two basic ways a function can cause side effects
gvalue = 0
def set_value(x):
global gvalue;
gvalue = x
def print_value():
print(gvalue)
set_value(3)
print_value()
set_value(5)
print_value()
3
5
impure function
def tail(s):
del s[0]
return s
def print_value():
a = [1, 2, 3]
b = tail(a)
print(b)
print(a)
print_value()
[2, 3]
[2, 3]
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 11/12
12/4/2020 Inheritance.ipynb - Colaboratory
A pure function must not alter the value of any data structure
def tail(s):
return s[1:]
def print_value():
a = [1, 2, 3]
b = tail(a)
print(b)
print(a)
print_value()
[2, 3]
[1, 2, 3]
https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 12/12