Unit-4 Inheritance

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

12/4/2020 Inheritance.

ipynb - Colaboratory

Following are the bene ts of inheritance.

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

#Line:1, definition of the superclass starts here


class Person:
#initializing the variables
name = ""
age = 0

#defining constructor
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge

#defining class methods


def showName(self):
print(self.name)

def showAge(self):
print(self.age)

# end of superclass definition

#definition of subclass starts here


class Student(Person): # Person is the superclass and Student is the subclass
studentId = ""

def __init__(self, studentName, studentAge, studentId):


self.studentId = studentId
Person.__init__(self, studentName, studentAge)

#Calling the superclass constructor and sending values of attributes.


#self.studentId = studentId

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

# Create an object of the superclass


person1 = Person("Richard", 23)
#call parent class method
print("Person age")
person1.showAge()
student1 = Student("Max", 22, "102")
# Create child class method
print("student details")
student1.showName()
student1.getId()
student1.showAge()

Person age
23
student details
Max
102
22

Another example:

class Polygon:

def __init__(self, no_of_sides):

self.n = no_of_sides

self.sides = [0 for i in range(no_of_sides)]

def inputSides(self):

self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in


range(self.n)]

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

# calculate the semi-perimeter

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)


t = Triangle()
t.inputSides()
t.dispSides()
t.findArea()

---------------------------------------------------------------------------
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

NameError: name 'a' is not defined

SEARCH STACK OVERFLOW

Use the super() Function

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()

greeting from child

Different types of inheritance

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.

3. Multilevel inheritance: When we have a child and grandchild relationship.


https://colab.research.google.com/drive/1AXY8E8-_CXFUki59ZZsZpc7SBDcWV4uJ#printMode=true 6/12
12/4/2020 Inheritance.ipynb - Colaboratory

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.

# Python example to show the working of multiple


# inheritance
class Base1():
def __init__(self):
self.str1 = "python"
print("Base1")

class Base2():
def __init__(self):
self.str2 = "Inheritance"
print("Base2")

class Derived(Base1, Base2):


def __init__(self):

# Calling constructors of Base1


# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")

def printStrs(self):
print(self.str1, self.str2)

ob = Derived()
ob.printStrs()

Base1
Base2
Derived
python Inheritance

Example for multilevel 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

# Inherited or Sub class (Note Person in bracket)


class Child(Base):

# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age

# To get name
def getAge(self):
return self.age

# Inherited or Sub class (Note Person in bracket)


class GrandChild(Child):

# 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.

Let's create a pure function to multiply numbers by 2:

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

original_numbers = [1, 3, 5, 10]


changed_numbers = multiply_2_pure(original_numbers)
print(original_numbers) # [1, 3, 5, 10]
print(changed_numbers) # [2, 6, 10, 20]

Impure functions

There are two basic ways a function can cause side effects

that directly affect other parts of the code. The rst is by

reading or writing global variables. For example:

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

that is passed into it. This version of tail is not pure. We

could create a pure version like this:

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

You might also like