9 Inheritance
9 Inheritance
1
Python | CCIT
Table of Contents
Inheritance ........................................................................... 3
Inheritance .......................................................................... 3
Method Overriding ............................................................. 9
Calling base class overrided method in derived class ...... 12
Super() function ................................................................ 15
Types of Inheritance .......................................................... 18
Single level Inheritance ..................................................... 18
Multi-level Inheritance ..................................................... 18
Hierarchical Inheritance ................................................... 18
Multiple Inheritance ......................................................... 18
Hybrid Inheritance ............................................................ 19
Method Resolution Order ................................................. 29
2
Python | CCIT
Inheritance
It is a technique of deriving new classes from
existing classes.
The derived class inherits all features of base class
from which it is derived.
In the derived class we only need to define
additional features or the changes that we
require.
So by using this technique we can reuse our
existing code.
This is called as code reusability.
Inheritance
A new class can be derived by providing list of base class names from which
we want to derive.
Syntax:
class DerivedClassName(Base1,Base2,. . ):
Methods
-------------
-------------
NOTE:
Every class in Python is derived from the class object.
If base class name is not provided then that classs is
derived from class object.
3
Python | CCIT
class circle:
def setradius(self,r):
self.r=r
def area(self):
a=3.14*self.r**2
print("Area is ",a)
class xcircle(circle):
def circumference(self):
c=2*3.14*self.r
print("circumference is ",c)
a=circle()
a.setradius(5)
a.area()
b=xcircle()
b.setradius(2)
b.area()
b.circumference()
Area is 78.5
Area is 6.28
Circumference is 6.28
4
Python | CCIT
Design a class Employee data member name, salary, advance, member functions
setdata (n,s ,a), showdata( )
Then derive a new class AEmployee from Employee: Member functions Payment()
class Employee:
def setData(self,n,s,a):
self.name=n
self.salary=s
self.advance=a
def showData(self):
print("Name is ",self.name)
print("Salary is ",self.salary)
print("Advance is ",self.advance)
class AEmployee(Employee):
def payment(self):
p=self.salary - self.advance
print("Payment is ",p)
a=Employee()
a.setData("Amit",25000,5000)
a.showData()
b=AEmployee()
b.setData("Gopal",40000,500)
b.showData()
b.payment()
5
Python | CCIT
Name is Amit
Salary is 25000
Advance is 5000
Name is Gopal
Salary is 40000
Advance is 500
Payment is 39500
class Account:
def open(self,an,bl):
self.accno=an
self.bal=bl
def deposit(self,amt):
self.bal=self.bal+amt
def withdraw(self,amt):
self.bal=self.bal-amt
def showBalance(self):
print("AccNo is ",self.accno)
print("Bal is ",self.bal)
class SAccount(Account):
def addInterest(self,r,n):
si=self.bal*r*n/100
self.bal=self.bal+si
6
Python | CCIT
a=Account()
a.open(4117,25000)
a.deposit(3000)
a.withdraw(8000)
a.showBalance()
b=SAccount()
b.open(3012,10000)
b.deposit(40000)
b.showBalance()
b.addInterest(10.25,3)
b.showBalance()
AccNo is 4117
Balance is 20000
AccNo is 3012
Balance is 50000
AccNo is 3012
Balance is 65375.50
7
Python | CCIT
class Rectangle :
def setDimension(self,x,y):
self.length = x
self.breadth = y
def area(self):
a=self.length * self.breadth
print("Area is ",a)
class ARectangle(Rectangle):
def perimeter(self):
p=2*(self.length + self.breadth)
print("Perimeter is ",p)
a = Rectangle()
a.setDimension(5,7)
a.area()
b = ARectangle()
b.setDimension(10,20)
b.area()
b.perimeter()
Area is 35
Area is 200
Perimeter is 60
8
Python | CCIT
Method Overriding
Redefining a base class method in derived class is called as method overriding.
The overrided method must have same name as base class method.
This overrided method will be call for derived class object instead of base class
inherited method.
class Box:
def setdata(self,x,y,z):
self.L=x
self.B=y
self.H=z
def volume(self):
v=self.L*self.B*self.H
print("Volume is ",v)
def surfacearea(self):
a=2*self.L*self.H+2*self.B*self.H+2*self.L*self.B
print("SurfaceArea is ",a)
class OpenBox(Box):
def surfacearea(self):
a=2*self.L*self.H+2*self.B*self.H+self.L*self.B
print("SurfaceArea is ",a)
9
Python | CCIT
a=Box()
a.setdata(2,2,2)
a.volume()
a.surfacearea()
b=OpenBox()
b.setdata(1,1,1)
b.volume()
b.surfacearea()
Volume is 8
Surface Area is 24
Volume is 1
Surface Area is 5
class Account:
def open(self,an,bl):
self.accno=an
self.balance=bl
def deposit(self,amt):
self.balance=self.balance+amt
def withdraw(self,amt): 10
Python | CCIT
self.balance=self.balance-amt
def showBalance(self):
print("AccNo is ",self.accno)
print("Balance is",self.balance)
class CAccount(Account):
def deposit(self,amt):
self.balance=self.balance+amt
self.balance=self.balance -1
def withdraw(self,amt):
self.balance=self.balance-amt
self.balance=self.balance -1
a=Account()
a.open(4117,25000)
a.deposit(3000)
a.withdraw(8000)
a.showBalance()
b=CAccount()
b.open(3012,10000)
b.deposit(5000)
b.withdraw(2000)
b.withdraw(3000)
b.showBalance()
11
Python | CCIT
AccNo is 4117
Balance is 20000
AccNo is 3012
Balance is 9997
class Account:
def open(self,an,bl):
self.accno=an
self.balance=bl
def deposit(self,amt):
self.balance=self.balance+amt
def withdraw(self,amt):
self.balance=self.balance-amt
def showBalance(self):
print("AccNo is ",self.accno)
print("Balance is",self.balance)
12
Python | CCIT
class CAccount(Account):
def deposit(self,amt):
Account.deposit(self,amt)
self.balance=self.balance-1
def withdraw(self,amt):
Account.withdraw(self,amt)
self.balance=self.balance-1
a=Account()
a.open(4117,25000)
a.deposit(3000)
a.withdraw(8000)
a.showBalance()
b=CAccount()
b.open(3012,10000)
b.deposit(5000)
b.withdraw(2000)
b.withdraw(3000)
b.showBalance()
AccNo is 4117
Balance is 20000
AccNo is 3012
Balance is 9997
13
Python | CCIT
class Person:
def setdata(self,name,city):
self.name=name
self.city=city
def showdata(self):
print("Name is ",self.name)
print("City is ",self.city)
class Student(Person):
def setdata(self,rollno,name,city):
self.rollno=rollno
Person.setdata(self,name,city)
def showdata(self):
print("RollNo is",self.rollno)
Person.showdata(self)
a=Person()
a.setdata("Amit jain","Amravati")
a.showdata()
b=Student()
b.setdata(4117,"Gopal Pandey" ,"Nagpur")
b.showdata( ) 14
Python | CCIT
RollNo is 4117
Name is Gopal Pandey
City is Nagpur
Super( ) function
It allows us to refer to the parent class explicitly.
It’s useful in case of inheritance where we want to call super/base class
functions.
Syntax:
super().baseClassFnName(arg1..)
super().setdata(name,city)
def showdata(self):
print("RollNo is",self.rollno)
super().showdata()
a=Person()
a.setdata("Amit jain","Amravati")
a.showdata()
b=Student()
b.setdata(4117,"Gopal Pandey" ,"Nagpur")
b.showdata( )
RollNo is 4117
Name is Gopal Pandey
City is Nagpur
16
Python | CCIT
Example:
class Person:
def __init__(self,name,city):
self.name=name
self.city=city
def showdata(self):
print("Name is ",self.name)
print("City is ",self.city)
class Student(Person):
def __init__(self,rollno,name,city):
self.rollno=rollno
super().__init__(name,city)
def showdata(self):
print("RollNo is",self.rollno)
super().showdata()
a=Person("Amit jain","Amravati")
a.showdata()
b=Student(4117,"Gopal Pandey" ,"Nagpur")
b.showdata( )
RollNo is 4117
Name is Gopal Pandey
City is Nagpur
17
Python | CCIT
Types of Inheritance
Multi-level Inheritance
If a class is derived from a derived class then such type of
inheritance is called as multi-level inheritance.
Hierarchical Inheritance
If multiple classes are derived from a single
base class then such type of inheritance is
called as Hierarchical inheritance.
Multiple Inheritance
If a class is derived from a multiple base
classes then such type of inheritance is
called as multiple inheritance.
18
Python | CCIT
Hybrid Inheritance
If mix type of inheritance is used then such type
of inheritance is called as Hybrid inheritance.
class Person:
def __init__(self,nm,ct):
self.name = nm
self.city = ct
def showdata(self):
print("Name is ", self.name)
print("City is ", self.city)
class Employee(Person):
def __init__(self,nm,ct,jb,sl):
super().__init__(nm,ct)
self.job = jb
self.salary = sl
def showdata(self):
super().showdata()
print("Job is ",self.job)
print("Salary is ", self.salary)
19
Python | CCIT
class Manager(Employee):
def __init__(self,nm,ct,jb,sl,br):
super().__init__(nm,ct,jb,sl)
self.branch = br
def showdata(self):
super().showdata()
print("Branch is ",self.branch)
a=Person("Amit jain","Amrvati")
a.showdata()
b=Employee("Raj Rathi","Raipur","Clerk",15600)
b.showdata()
c=Manager("M.Rao","Mumbai","Sr.Mgr",45600,"SBI-Amt")
c.showdata()
Name is M.Rao
City is Mumbai
Job is Sr.Mgr
Salary is 45600
Branch is SBI-Amt
20
Python | CCIT
Design 3 classes Student, Exam and Marksheet from given inheritance diagram
class Student:
def __init__(self,rn,nm,br):
self.rollno=rn
self.name = nm
self.branch = br
def showdata(self):
print("RollNo is",self.rollno)
print("Name is" ,self.name)
print("Branch is" ,self.branch)
class Exam(Student):
def __init__(self,r,n,b,*mks):
super().__init__(r,n,b)
self.marks = mks
class Marksheet(Exam):
def total(self):
t=sum(self.marks)
return t
def percentage(self):
c=len(self.marks)
p=sum(self.marks)/c
return p
21
if mk < 40 :
return "Fail"
Python | CCIT
def result(self):
for mk in self.marks:
if mk < 40 :
return "Fail"
else:
return "Pass"
a=Marksheet(4117,"Amit Jain","CSE-3",57,68,34,56,62)
a.showdata()
print("Total is",a.total())
print("Prcentage is",a.percentage())
print("Result is",a.result())
RollNo is 4117
Name is Amit Jain
Branch is CSE-3
Total is 277
Prcentage is 55.4
Result is Fail
22
Python | CCIT
Design 3 classes Person Student and Teacher from following inheritance Diagram.
class Person:
def __init__(self,nm,ct):
self.name=nm
self.city=ct
def showdata(self):
print("Name is ",self.name)
print("City is ",self.city)
class Student(Person):
def __init__(self,rn,nm,ct,br):
self.rollno=rn
super().__init__(nm,ct)
self.branch=br
def showdata(self):
print("Rollno is",self.rollno)
super().showdata()
print("Branch is",self.branch) 23
Python | CCIT
class Teacher(Person):
def __init__(self,nm,ct,*subs):
super().__init__(nm,ct)
elf.subjects=subs
def showdata(self):
super().showdata()
print("Subjects are")
for sub in self.subjects:
print(sub)
a=Person("Amit jain","Amrvati")
a.showdata()
b=Student(4117,"Raj Joshi","Nagpur","CSE-3")
b.showdata()
c=Teacher("Raja Kumar","Raipur","Hin","His","Geo")
c.showdata()
Design 3 classes Person Student and Teacher from following inheritance Diagram.
class Person:
def __init__(self,nm,ct):
self.name=nm
self.city=ct
def showdata(self):
print("Name is ",self.name)
print("City is ",self.city)
class Employee(Person):
def __init__(self,nm,ct,jb,sl,ad):
super().__init__(nm,ct)
self.job=jb
self.salary=sl
self.advance=ad
def showdata(self):
super().showdata()
print("Job is ",self.job) 25
print("Salary is ",self.salary)
Python | CCIT
print("Salary is ",self.salary)
print("Advance is ",self.advance)
def payment(self):
p=self.salary-self.advance
print("payment is ",p)
class Worker(Person):
def __init__(self,nm,ct,wg,wd):
super().__init__(nm,ct)
self.wages=wg
self.wdays=wd
def showdata(self):
super().showdata()
print("Wages is ",self.wages)
print("Wdays is ",self.wdays)
def payment(self):
p=self.wages*self.wdays
print("payment is ",p)
a=Person("Amit jain","Amrvati")
a.showdata()
b=Employee("Raj Rathi","Nagpur","Clerk",25000,5000)
b.showdata()
b.payment()
c=Worker("Raja Kumar","Raipur",500,25)
c.showdata()
c.payment()
26
Python | CCIT
Design 3 classes Person Test and Student from given Inheritance diagram.
27
Python | CCIT
class Person:
def __init__(self,name,city):
self.name=name
self.city=city
def showdata(self):
print("Name is ",self.name)
print("City is ",self.city)
class Test:
def __init__(self,subject,marks):
self.subject=subject
self.marks=marks
def showdata(self):
print("Subject is ",self.subject)
print("Marks are ",self.marks)
class Student(Person,Test):
def __init__(self,rollno,name,city,subject,marks):
self.rollno=rollno
Person.__init__(self,name,city)
Test.__init__(self,subject,marks)
def showdata(self):
print("RollNo is",self.rollno)
Person.showdata(self)
Test.showdata(self)
a=Student(4117,"Raj Rathi","Amravati","CET",55)
a.showdata()
28
Python | CCIT
RollNo is 4117
Name is Raj Rathi
City is Amravati
Subject is CET
Marks are 55
29
Python | CCIT
class Person:
def __init__(self,n,c):
self.name=n
self.city=c
def showData(self):
print("Name is",self.name)
print("City is",self.city)
class Employee:
def __init__(self,s,a):
self.salary=s
self.advance=a
def showData(self):
print("Salary is",self.salary)
print("Advance is",self.advance)
def payment(self): 30
p=self.salary-self.advance
Python | CCIT
p=self.salary-self.advance
print("Payment is",p)
class Programmer(Person,Employee):
def __init__(self,n,c,s,a,*langs):
Person.__init__(self,n,c)
Employee.__init__(self,s,a)
self.languages=langs
def showData(self):
Person.showData(self)
Employee.showData(self)
print("Languages",self.languages)
a=Programmer("Raj Rathi","Nagpur",45500,5000,"C","C++",
"Java")
a.showData()
a.payment()
31
Python | CCIT
32