SlideShare a Scribd company logo
BCS-DS-427B: PYTHON
Ms. Swati Hans
Assistant Professor, CSE
Manav Rachna International Institute of Research and Studies, Faridabad
1
PYTHON
Unit-4
INHERITANCE, ITS
TYPES
INHERITANCE
Inheritance is the capability of one class to derive or inherit the properties
from some another class.
Parent class is termed as base class
Child class is also termed as derived class
The benefits of inheritance are:
▪Code reusability
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.
▪Transitivity
It means that if class B inherits from another class A, then all the subclasses
of B would automatically inherit from class A.
SINGLE LEVEL INHERITANCE
When a child class inherits from only
one parent class which is not derived
from any other class, it is called as
single level inheritance.
Here A is a base class, B is child class of
A as it is inheriting features of A
class.55d xc ./
+
A
B
SINGLE LEVEL INHERITANCE
CODE
class A:
def funA(self):
print("in A")
class B(A):
def funB(self):
print("in B")
b1=B()
b1.funA
b1.funB()
Here,B class is inheriting class A .So class A
functions will
Become members of class B.
So,when object b1 of class B is created, all
functions of
Class A and B are called from it.
MULTILEVEL INHERITANCE
Multi-level inheritance is when a
derived class inherits another derived
class. There is no limit on the number
of levels up to which, the multi-level
inheritance can be achieved.
Here Person is a base class, Employee
is child class of Person and Manager is
a child class of Employee.
Person
Employee
Manager
MULTILEVEL INHERITANCE
CODE
class Person:
name=""
def getName(self): # To get
name
return self.name
def setName(self,s): # To set
the name
self.name=s
class Manager(Employee):
projectstatus=""
def getProjectStatus(self): # To get
project status
return self.projectstatus
def setProjectStatus(self,st): # To set
project status
self.projectstatus=st
Here,we created three classes
Employee class is inheriting Person class and
Manager class
is inheriting Employee class.
class Employee(Person):
salary=0
def getSalary(self): # To get
salary
return self.salary
def setSalary(self,num): # To set
#Python Program to depict multilevel inheritance
MULTILEVEL CONTINUE…..
Output
m=Manager()
m.setName("Rahul") # calling its setter methods to initialize variables
m.setSalary(155000)
m.setProjectStatus("Half completed")
print("Name is ",m.getName()) # calling all getter functions
print("Salary is ",m.getSalary())
print("Project status is ",m.getProjectStatus())
Creating object of child class and calling its functions
MULTIPLE INHERITANCE
When child class inherits
features of more than
one base class.
Here ,Spacecraft is a child
class
which inherits Antennas
and Power Base classes
Antennas Power
Spacecraft
MULTIPLE INHERITANCE CODE
class Power:
solarcells=0
nickelbattery=0
def setcells_battery(self,a,b):
self.solarcells=a
self.nickelbattery=b
class Spacecraft(Antennas,Power):
mission=''
speed=0
def setMission(self,x):
self.mission=x
def setSpeed(self,y):
self.speed=y
def display(self):
print("Mission is",self.mission)
print("Speed is",self.speed)
print("Antenna diameter
is",self.diameter)
print("Solarcells are ",self.solarcells)
print("Nickle batteries are",
class Antennas:
diameter=0
def setDiameter(self,a):
self.diameter=a
# Python Program to depict multiple inheritance
MULTIPLE INHERITANCE EXAMPLE CODE…
s1 = Spacecraft() # creating space craft object
s1.setMission("Weather Forecasting") # call its setter methods t initialize variables
s1.setSpeed(61000)
s1.setDiameter(6)
s1.setcells_battery(3200,2)
s1.display() # call its display method
Outp
ut
Creating object of child class and calling its functions
USING SUPER() TO CALL
PARENT CLASS CONSTRUCTOR
Any child class can call constructor i.e __init__(..) of parent class i.e Super
class by making use of super.
Syntax:
super().__init__(parameters of base class constructor)
For example, In case of multilevel inheritance ,if A B C
🡪 🡪
If A is parent of B and B is parent of C,Then C can call constructor of B
using siuper and B can call constructor of A using super
EXAMPLE: USING SUPER() IN MULTILEVEL
INHERITANCE
class Vehicle:
vnum=0
def __init__(self,x):
self.vnum=x
def vdisplay(self):
print("Vehicle Number: ",self.vnum)
Vehicle
Car
BulletCar
BulletCar calling Car
constructor using
super()
Car calling Vehicle
constructor using
super()
class Car(Vehicle):
engine=""
def __init__(self,s,n):
self.engine=s
super().__init__(n)
def cdisplay(self):
print("Car engine:",self.engine)
EXAMPLE CONTINUED
class BulletCar(Car):
company=""
def __init__(self,c,s,n):
self.company=c
super().__init__(s,n) #calling Car constructor
def bcardisplay(self):
print("Bullet Car Company :",self.company)
Outp
ut
As ,init() of BulletCar is calling init() of Car
As, Init() of Car is calling init() of Vehicle
When we create obj of BulletCar() ,its init
will
be called automatically.Those constructors
will be
called in sequence
BulletCar init() Car init() Vehicle init().
🡪 🡪
Functions of all parent classes can be
called from
obj=BulletCar("JCBL","Diesel","HR1809“)
# object created
obj.vdisplay() # Vehicle function called
obj.cdisplay() # Car function called
obj.bcardisplay() #BulletCar function called
MRO(METHOD RESOLUTION ORDER)
I
Method Resolution Order (MRO) is the order in which Python looks for a method in a hierarchy of
classes. Especially it plays vital role in the context of multiple inheritance as single method may be
found in multiple super classes.
In the multiple inheritance scenario, any specified attribute is searched first in the current class.
If not found, the search continues into parent classes in depth-first, left-right fashion without
searching same class twice and then finally in object class. As every class in Python is derived
from the class object.
So, In above example , MRO will be
GermanShephard, Dog, GoodAnimals, Object
Dog GoodAnimals
GermanShepar
d
MRO OF A CLASS(In case of Multiple Inheritance):
class GoodAnimals(object):
def __init__(self):
print("They're all good dogs")
super().__init__()
class GermanShepard(Dog,GoodAnimals):
def __init__(self):
print("init GermanShepard")
super().__init__() # calling base class
constructor
obj = GermanShepard()
print(GermanShephard.__mro__)
print(GermanShephard.mro())
MRO of a class can be viewed by using __mro__ attribute or mro() method. Create class Dog,GoodAnimals
, GermanShephard inherits Dog and Goodanimals. Then display MRO of GermanShephard
class Dog:
def __init__(self):
print("init Dog")
super().__init__()
MRO IN CASE OF MULTIPLE INHERITANCE
class GoodAnimals(object):
def __init__(self):
print("They're all good dogs")
super().__init__()
class Dog:
def __init__(self):
print("init Dog")
super().__init__() #pass the call to
#next class in
#MRO
class GermanShepard(Dog,GoodAnimals):
def __init__(self):
print("init GermanShepard")
super().__init__() # calling base class
constructor
obj = GermanShepard()
Output
• When the GermanShepard object is created,
MRO searches for __init__() firstly in
GermanShepard itself.
• GermanShepard init is calling super init() will
call
init of next class in MRO i.e. Dog class init
• Dog class init is calling super init() again
which will call
init of next class in MRO i.e. GoodAnimals
class init
In multiple inheritance, if you want constructor of child should call constructor of all
parent classes.
Then all parent and Child classes should call super().__init__()

More Related Content

PDF
Object oriented Programning Lanuagues in text format.
PDF
Python programming : Inheritance and polymorphism
PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Inheritance in java
PDF
Inheritance in Java.pdf
PPTX
Inheritance in Java beginner to advance with examples.pptx
PDF
Inheritance
PPTX
Object oriented Programning Lanuagues in text format.
Python programming : Inheritance and polymorphism
Inheritance,single,multiple.access rulepptx
Inheritance in java
Inheritance in Java.pdf
Inheritance in Java beginner to advance with examples.pptx
Inheritance

Similar to Inheritance Super and MRO _ (20)

PDF
java inheritance that is used in oop cls
PDF
All about python Inheritance.python codingdf
PPTX
Inheritance
PPTX
Inheritance in java
DOCX
Ganesh groups
PPTX
Inheritance in c++
PDF
inheritance-16031525566nbhij56604452.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision (3).pdf
PPTX
inheritance and interface in oops with java .pptx
PPTX
Python programming computer science and engineering
PPTX
Basics to java programming and concepts of java
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PDF
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
Inheritance Interface and Packags in java programming.pptx
PPTX
Inheritance in Java
java inheritance that is used in oop cls
All about python Inheritance.python codingdf
Inheritance
Inheritance in java
Ganesh groups
Inheritance in c++
inheritance-16031525566nbhij56604452.pdf
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision (3).pdf
inheritance and interface in oops with java .pptx
Python programming computer science and engineering
Basics to java programming and concepts of java
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance Interface and Packags in java programming.pptx
Inheritance in Java
Ad

More from swati463221 (13)

PPTX
python ..... _
PPTX
python _
PPTX
matplotlib _
PPTX
image processing _
PPTX
numpy2 _
PPTX
Classes and Objects _
PDF
Function overloading or Polymorphism.pdf
PPTX
Classes & Objects _
PPTX
kruskal and prims algorithm _
PPT
switching.ppt
PPT
stop and wait
PPTX
subnetting
PPTX
FIREWALL
python ..... _
python _
matplotlib _
image processing _
numpy2 _
Classes and Objects _
Function overloading or Polymorphism.pdf
Classes & Objects _
kruskal and prims algorithm _
switching.ppt
stop and wait
subnetting
FIREWALL
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Welding lecture in detail for understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
web development for engineering and engineering
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Construction Project Organization Group 2.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
bas. eng. economics group 4 presentation 1.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Welding lecture in detail for understanding
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT 4 Total Quality Management .pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
OOP with Java - Java Introduction (Basics)
web development for engineering and engineering
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Construction Project Organization Group 2.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
additive manufacturing of ss316l using mig welding
bas. eng. economics group 4 presentation 1.pptx

Inheritance Super and MRO _

  • 1. BCS-DS-427B: PYTHON Ms. Swati Hans Assistant Professor, CSE Manav Rachna International Institute of Research and Studies, Faridabad 1
  • 3. INHERITANCE Inheritance is the capability of one class to derive or inherit the properties from some another class. Parent class is termed as base class Child class is also termed as derived class The benefits of inheritance are: ▪Code reusability 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. ▪Transitivity It means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
  • 4. SINGLE LEVEL INHERITANCE When a child class inherits from only one parent class which is not derived from any other class, it is called as single level inheritance. Here A is a base class, B is child class of A as it is inheriting features of A class.55d xc ./ + A B
  • 5. SINGLE LEVEL INHERITANCE CODE class A: def funA(self): print("in A") class B(A): def funB(self): print("in B") b1=B() b1.funA b1.funB() Here,B class is inheriting class A .So class A functions will Become members of class B. So,when object b1 of class B is created, all functions of Class A and B are called from it.
  • 6. MULTILEVEL INHERITANCE Multi-level inheritance is when a derived class inherits another derived class. There is no limit on the number of levels up to which, the multi-level inheritance can be achieved. Here Person is a base class, Employee is child class of Person and Manager is a child class of Employee. Person Employee Manager
  • 7. MULTILEVEL INHERITANCE CODE class Person: name="" def getName(self): # To get name return self.name def setName(self,s): # To set the name self.name=s class Manager(Employee): projectstatus="" def getProjectStatus(self): # To get project status return self.projectstatus def setProjectStatus(self,st): # To set project status self.projectstatus=st Here,we created three classes Employee class is inheriting Person class and Manager class is inheriting Employee class. class Employee(Person): salary=0 def getSalary(self): # To get salary return self.salary def setSalary(self,num): # To set #Python Program to depict multilevel inheritance
  • 8. MULTILEVEL CONTINUE….. Output m=Manager() m.setName("Rahul") # calling its setter methods to initialize variables m.setSalary(155000) m.setProjectStatus("Half completed") print("Name is ",m.getName()) # calling all getter functions print("Salary is ",m.getSalary()) print("Project status is ",m.getProjectStatus()) Creating object of child class and calling its functions
  • 9. MULTIPLE INHERITANCE When child class inherits features of more than one base class. Here ,Spacecraft is a child class which inherits Antennas and Power Base classes Antennas Power Spacecraft
  • 10. MULTIPLE INHERITANCE CODE class Power: solarcells=0 nickelbattery=0 def setcells_battery(self,a,b): self.solarcells=a self.nickelbattery=b class Spacecraft(Antennas,Power): mission='' speed=0 def setMission(self,x): self.mission=x def setSpeed(self,y): self.speed=y def display(self): print("Mission is",self.mission) print("Speed is",self.speed) print("Antenna diameter is",self.diameter) print("Solarcells are ",self.solarcells) print("Nickle batteries are", class Antennas: diameter=0 def setDiameter(self,a): self.diameter=a # Python Program to depict multiple inheritance
  • 11. MULTIPLE INHERITANCE EXAMPLE CODE… s1 = Spacecraft() # creating space craft object s1.setMission("Weather Forecasting") # call its setter methods t initialize variables s1.setSpeed(61000) s1.setDiameter(6) s1.setcells_battery(3200,2) s1.display() # call its display method Outp ut Creating object of child class and calling its functions
  • 12. USING SUPER() TO CALL PARENT CLASS CONSTRUCTOR Any child class can call constructor i.e __init__(..) of parent class i.e Super class by making use of super. Syntax: super().__init__(parameters of base class constructor) For example, In case of multilevel inheritance ,if A B C 🡪 🡪 If A is parent of B and B is parent of C,Then C can call constructor of B using siuper and B can call constructor of A using super
  • 13. EXAMPLE: USING SUPER() IN MULTILEVEL INHERITANCE class Vehicle: vnum=0 def __init__(self,x): self.vnum=x def vdisplay(self): print("Vehicle Number: ",self.vnum) Vehicle Car BulletCar BulletCar calling Car constructor using super() Car calling Vehicle constructor using super() class Car(Vehicle): engine="" def __init__(self,s,n): self.engine=s super().__init__(n) def cdisplay(self): print("Car engine:",self.engine)
  • 14. EXAMPLE CONTINUED class BulletCar(Car): company="" def __init__(self,c,s,n): self.company=c super().__init__(s,n) #calling Car constructor def bcardisplay(self): print("Bullet Car Company :",self.company) Outp ut As ,init() of BulletCar is calling init() of Car As, Init() of Car is calling init() of Vehicle When we create obj of BulletCar() ,its init will be called automatically.Those constructors will be called in sequence BulletCar init() Car init() Vehicle init(). 🡪 🡪 Functions of all parent classes can be called from obj=BulletCar("JCBL","Diesel","HR1809“) # object created obj.vdisplay() # Vehicle function called obj.cdisplay() # Car function called obj.bcardisplay() #BulletCar function called
  • 15. MRO(METHOD RESOLUTION ORDER) I Method Resolution Order (MRO) is the order in which Python looks for a method in a hierarchy of classes. Especially it plays vital role in the context of multiple inheritance as single method may be found in multiple super classes. In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice and then finally in object class. As every class in Python is derived from the class object. So, In above example , MRO will be GermanShephard, Dog, GoodAnimals, Object Dog GoodAnimals GermanShepar d
  • 16. MRO OF A CLASS(In case of Multiple Inheritance): class GoodAnimals(object): def __init__(self): print("They're all good dogs") super().__init__() class GermanShepard(Dog,GoodAnimals): def __init__(self): print("init GermanShepard") super().__init__() # calling base class constructor obj = GermanShepard() print(GermanShephard.__mro__) print(GermanShephard.mro()) MRO of a class can be viewed by using __mro__ attribute or mro() method. Create class Dog,GoodAnimals , GermanShephard inherits Dog and Goodanimals. Then display MRO of GermanShephard class Dog: def __init__(self): print("init Dog") super().__init__()
  • 17. MRO IN CASE OF MULTIPLE INHERITANCE class GoodAnimals(object): def __init__(self): print("They're all good dogs") super().__init__() class Dog: def __init__(self): print("init Dog") super().__init__() #pass the call to #next class in #MRO class GermanShepard(Dog,GoodAnimals): def __init__(self): print("init GermanShepard") super().__init__() # calling base class constructor obj = GermanShepard() Output • When the GermanShepard object is created, MRO searches for __init__() firstly in GermanShepard itself. • GermanShepard init is calling super init() will call init of next class in MRO i.e. Dog class init • Dog class init is calling super init() again which will call init of next class in MRO i.e. GoodAnimals class init In multiple inheritance, if you want constructor of child should call constructor of all parent classes. Then all parent and Child classes should call super().__init__()