SlideShare a Scribd company logo
CHAPTER - 07
EXTENDING CLASSES - INHERITANCE
Unit I
Programming and Computational
Thinking (PCT-2)
(80 Theory + 70 Practical)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
Class XII
INTRODUCTION – INHERITANCE
INHERITANCE
In object oriented programming,
inheritance is a mechanism in which a new
class is derived from an already defined
class. The derived class is known as a
subclass or a child class. The pre-existing
class is known as base class or a parent class
or a super class. The mechanism of
inheritance gives rise to hierarchy in classes.
The major purpose of inheriting a base class
into one or more derived class is code reuse.
INHERITANCE - OVERRIDING
What is Overriding?
The subclass inherits all the methods
and properties of the super class. The
subclass can also create its own methods
and replace methods of the superclass. The
process of replacing methods defined in
super with new methods with same name in
the derived class is known as overriding.
TYPES OF INHERITANCE
Inheritance can be categorised into five types:
1. SINGLE INHERITANCE
2. MULTILEVEL INHERITANCE
3. MULTIPLE INHERITANCE
4. HIERARCHICAL INHERITANCE
5. HYBRID INHERITANCE
1. SINGLE INHERITANCE
This is the simplest kind of inheritance.
In this, a subclass is derived from a single
base class.
BASE
DERIVED
2. MULTILEVEL INHERITANCE
In this type of inheritance, the derived
class becomes the base of another class.
BASE 1
DERIVED
BASE 2
3. MULTIPLE INHERITANCE
BASE 1
DERIVED
BASE 2
In this type of inheritance, the derived
class inherits from one or more base classes.
4. HIERARCHICAL INHERITANCE
In this type of inheritance, the base
class is inherited by more than one class.
BASE 1
DERIVED 2DERIVED 1
5. HYBRID INHERITANCE
This inheritance is a combination of multiple,
hierarchical and multilevel inheritance.
BASE 1
BASE 2
DERIVED 2DERIVED 1
EXAMPLES ON INHERITANCE
EXAMPLES ON INHERITANCE
Syntax:
class subclass (super):
For Example
class person:
def __init__(self,name,age):
self.name=name
self.age=age
def getName(self):
return self.name
EXAMPLES ON INHERITANCE
def getAge(self):
return self.Age
class student(person):
def __init__(self,name,age,rollno,marks):
super(student,self)._init_(self, name, age)
self.rollno=rollno
self.marks=marks
def getRoll(self):
return self.rollno
def getMarks(self):
return self.Marks
EXAMPLES ON INHERITANCE
The above example implements single
inheritance. The class student extends the class
person. The class student adds two instance
variables rollno and marks. In order to add new
instance variables, the _init_() method defined
in class person needs to be extended.
The __init__() function of subclass student
initializesname and age attributes of superclass
person and also create new attributes rollno and
marks.
EXTENDING __init__ () METHOD
EXTENDING __init__ () METHOD
In python the above task of extending
__init__ () can be achieved the following ways:
i) By using super() function
ii) By using name of the super class.
super() FUNCTION
super() FUNCTION
In the above example, the class student
and class person both have __init__ () method.
The __init__ () method is defined in cIass
person and extended in class student. In
Python, super() function is used to call the
methods of base class which have been
extended in derived class
Syntax:
super(type, variable) bound object
super() FUNCTION EXAMPLE
class student(person):
def __init__(self,name,age,rollno,marks):
super(student, self).__init__ (self, name, age)
self.rollno=rollno
self.marks=marks
BY USING NAME OF THE SUPER CLASS
BY USING NAME OF THE SUPER CLASS
As discussed above, the class student and
class person both have __init__ () method. The
__init__ () method is defined in cIass person and
extended in class student. In Python, name of
the base class can also be used to access the
method of the base class which has been
extended in derived class.
BY USING NAME OF THE SUPER CLASS
Example
class student(person):
def __init__(self,name,age,rollno,marks):
person.__init__ (self, name, age)
self.rollno=rollno
self.marks=marks
MULTIPLE INHERITANCE EXAMPLE
MULTIPLE INHERITANCE EXAMPLE
Python supports a limited form of multiple
inheritance as well. A class definition with
multiple base classes looks like this
class SubClassName( Base1, Base2, Base3):
<statement1>
.
.
.
<statement N>
MULTIPLE INHERITANCE EXAMPLE
Python supports a limited form of multiple
inheritance as well. A class definition with
multiple base classes looks like this
class SubClassName( Base1, Base2, Base3):
<statement1>
.
.
.
<statement N>
MULTIPLE INHERITANCE EXAMPLE
For old-style classes, the only rule is
depth-first, left-to-right. Thus, if an attribute is
not found in SubClassName, it is searched in
Base1, then (recursively) in the base classes of
Base1, and only if it is not found there, it is
searched in Base2, and so on
MULTIPLE INHERITANCE EXAMPLE
For Example:
class student(object):
def __init__(self,Id,name):
self.Id=Id
self.name=name
def getName(self):
return self.name
def getId(self):
return self.Id
def show(self):
print self.name
MULTIPLE INHERITANCE EXAMPLE
print self.Id
class Teacher():
def __init__(self,tec_Id,tec_name, subject):
self.tec_Id=tec_Id
self.tec_name=tec_name
self.subject=subject
def getName(self):
return self.tec_name
def getId(self):
return self.tec_Id
MULTIPLE INHERITANCE EXAMPLE
def getSubject(self):
return self.ubject
def show(self):
print self. tec_name
print self.tec_Id
print self.subject
class school(student,Teacher):
def __init__(self, ID, name, tec_Id, tec_name, subject, sch_Id):
student.__init__ (self,ID,name)
Teacher. __init__(self,tec_Id,tec_name, subject)
self.sch_Id= sch_Id
MULTIPLE INHERITANCE EXAMPLE
def getId(self ):
return self.sch_Id
def display(self):
return self.sch_Id
In above example class school inherits class
student and teacher.
Let us consider these outputs
>>> s1=school(3,"Sham",56,"Ram","FIT",530)
>>> s1.display()
530
MULTIPLE INHERITANCE EXAMPLE
>>> s1.getId()
530
>>> s1.show()
Sham
3
The object of class school takes six
instance variables. The first five instance
variables have been defined in the base classes
(school and Teacher). The sixth instance
variable is defined in class school.
MULTIPLE INHERITANCE EXAMPLE
s1.display()displays the sch_Id and s1.getId()
also returns the sch_id. The classes school and
Teacher both have the method show (). But as
shown in above, the objects1 of class school
access the method of class student. This
because of depth-first, left-to-right rule.
Also, method getId()of class school is
overriding the methods with same names in
the base classes.
OVERRIDING METHODS
The feature of overriding methods
enables the programmer to provide specific
implementation to a method in the subclass
which is already implemented in the superclass.
The version of a method that is executed will be
determined by the object that is used to invoke
it. If an object of a parent class is used to invoke
the method, then the version in the parent class
will be executed, but if an object of the subclass
is used to invoke the method, then the version
in the child class will be executed.
OVERRIDING METHODS
Example 1:
class student(person):
def __init__(self,name,age,rollno,marks):
super(student,self).__init__(self, name,
age)
self.rollno=rollno
self.marks=marks
def getRoll(self):
return self.rollno
def getMarks(self):
return self.Marks
OVERRIDING METHODS
def show(self):
print self.rollno
print self.marks
As shown in the above example class
student inherits class person. Both the classes
have a function show( ).
The function show() in student is
overriding function show() in person().
OVERRIDING METHODS
The object of class person will print name
and age. The object of class student will print
rollno and marks. In case it is required that
function show of class student should display
name, age, rollno and marks, we should make
the following change in class student
class student(person):
def __init__(self,name,age,rollno,marks):
super(student,self).__init__(self,name,age)
OVERRIDING METHODS
self.rollno=rollno
self.marks=marks
def getRoll(self):
return self.rollno
def getMarks(self):
return self.Marks
def show(self):
person.show( )
print self.rollno
print self.marks
OVERRIDING METHODS
CLASS TEST
1. What is inheritance?
2. What are the types of inheritance?
3. Explain Multiple Inheritance with suitable
example
4. What is super? Explain
5. Explain Overriding functions
Class : XII Time: 40 Min
Topic: Inheritance Max Marks: 20
Thank You

More Related Content

What's hot (20)

PPTX
Class, object and inheritance in python
Santosh Verma
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
File handling in Python
Megha V
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
PDF
Python exception handling
Mohammed Sikander
 
PPTX
Polymorphism in Python
Home
 
PPTX
Functions in python
colorsof
 
PDF
Functions and modules in python
Karin Lagesen
 
ODP
Python Modules
Nitin Reddy Katkam
 
PDF
Strings in python
Prabhakaran V M
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPSX
python Function
Ronak Rathi
 
PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
Class, object and inheritance in python
Santosh Verma
 
Python: Modules and Packages
Damian T. Gordon
 
File handling in Python
Megha V
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Python exception handling
Mohammed Sikander
 
Polymorphism in Python
Home
 
Functions in python
colorsof
 
Functions and modules in python
Karin Lagesen
 
Python Modules
Nitin Reddy Katkam
 
Strings in python
Prabhakaran V M
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Polymorphism in java
Elizabeth alexander
 
python Function
Ronak Rathi
 
OOPS In JAVA.pptx
Sachin33417
 
Functions in Python
Kamal Acharya
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Java exception handling
BHUVIJAYAVELU
 

Similar to Chapter 07 inheritance (20)

PPTX
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
PPTX
Ruby object model
Chamnap Chhorn
 
PPT
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
PPT
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
İbrahim Kürce
 
PPT
Eo gaddis java_chapter_09_5e
Gina Bullock
 
PPT
Eo gaddis java_chapter_09_5e
Gina Bullock
 
PPTX
Inheritance in java
chauhankapil
 
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
PPTX
Inheritance ppt
Nivegeetha
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPT
A457405934_21789_26_2018_Inheritance.ppt
RithwikRanjan
 
PPTX
METHOD OVERLOADING AND INHERITANCE INTERFACE
mohanrajm63
 
PPTX
Inheritance
PhD Research Scholar
 
PPTX
Inheritance in oop
MuskanNazeer
 
PDF
Java Inheritance
Rosie Jane Enomar
 
PDF
Inheritance used in java
TharuniDiddekunta
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PPTX
Inheritance in Java beginner to advance with examples.pptx
naeemcse
 
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
Ruby object model
Chamnap Chhorn
 
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
OCA Java SE 8 Exam Chapter 5 Class Design
İbrahim Kürce
 
Eo gaddis java_chapter_09_5e
Gina Bullock
 
Eo gaddis java_chapter_09_5e
Gina Bullock
 
Inheritance in java
chauhankapil
 
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Inheritance ppt
Nivegeetha
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
A457405934_21789_26_2018_Inheritance.ppt
RithwikRanjan
 
METHOD OVERLOADING AND INHERITANCE INTERFACE
mohanrajm63
 
Inheritance in oop
MuskanNazeer
 
Java Inheritance
Rosie Jane Enomar
 
Inheritance used in java
TharuniDiddekunta
 
Unit3 inheritance
Kalai Selvi
 
Inheritance in Java beginner to advance with examples.pptx
naeemcse
 
Ad

More from Praveen M Jigajinni (20)

PPTX
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
 
PPTX
Chapter 08 data file handling
Praveen M Jigajinni
 
PPTX
Chapter 04 object oriented programming
Praveen M Jigajinni
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPTX
Chapter 02 functions -class xii
Praveen M Jigajinni
 
PPTX
Unit 3 MongDB
Praveen M Jigajinni
 
PPTX
Chapter 17 Tuples
Praveen M Jigajinni
 
PPTX
Chapter 15 Lists
Praveen M Jigajinni
 
PPTX
Chapter 14 strings
Praveen M Jigajinni
 
PPTX
Chapter 13 exceptional handling
Praveen M Jigajinni
 
PPTX
Chapter 10 data handling
Praveen M Jigajinni
 
PPTX
Chapter 9 python fundamentals
Praveen M Jigajinni
 
PPTX
Chapter 8 getting started with python
Praveen M Jigajinni
 
PPTX
Chapter 7 basics of computational thinking
Praveen M Jigajinni
 
PPTX
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
PPTX
Chapter 5 boolean algebra
Praveen M Jigajinni
 
PPTX
Chapter 4 number system
Praveen M Jigajinni
 
PPTX
Chapter 3 cloud computing and intro parrallel computing
Praveen M Jigajinni
 
PPTX
Chapter 2 operating systems
Praveen M Jigajinni
 
PPTX
Chapter 1 computer fundamentals
Praveen M Jigajinni
 
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
 
Chapter 08 data file handling
Praveen M Jigajinni
 
Chapter 04 object oriented programming
Praveen M Jigajinni
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Unit 3 MongDB
Praveen M Jigajinni
 
Chapter 17 Tuples
Praveen M Jigajinni
 
Chapter 15 Lists
Praveen M Jigajinni
 
Chapter 14 strings
Praveen M Jigajinni
 
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Chapter 10 data handling
Praveen M Jigajinni
 
Chapter 9 python fundamentals
Praveen M Jigajinni
 
Chapter 8 getting started with python
Praveen M Jigajinni
 
Chapter 7 basics of computational thinking
Praveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
Chapter 5 boolean algebra
Praveen M Jigajinni
 
Chapter 4 number system
Praveen M Jigajinni
 
Chapter 3 cloud computing and intro parrallel computing
Praveen M Jigajinni
 
Chapter 2 operating systems
Praveen M Jigajinni
 
Chapter 1 computer fundamentals
Praveen M Jigajinni
 
Ad

Recently uploaded (20)

PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
infertility, types,causes, impact, and management
Ritu480198
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 

Chapter 07 inheritance

  • 1. CHAPTER - 07 EXTENDING CLASSES - INHERITANCE
  • 2. Unit I Programming and Computational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 4. INHERITANCE In object oriented programming, inheritance is a mechanism in which a new class is derived from an already defined class. The derived class is known as a subclass or a child class. The pre-existing class is known as base class or a parent class or a super class. The mechanism of inheritance gives rise to hierarchy in classes. The major purpose of inheriting a base class into one or more derived class is code reuse.
  • 5. INHERITANCE - OVERRIDING What is Overriding? The subclass inherits all the methods and properties of the super class. The subclass can also create its own methods and replace methods of the superclass. The process of replacing methods defined in super with new methods with same name in the derived class is known as overriding.
  • 6. TYPES OF INHERITANCE Inheritance can be categorised into five types: 1. SINGLE INHERITANCE 2. MULTILEVEL INHERITANCE 3. MULTIPLE INHERITANCE 4. HIERARCHICAL INHERITANCE 5. HYBRID INHERITANCE
  • 7. 1. SINGLE INHERITANCE This is the simplest kind of inheritance. In this, a subclass is derived from a single base class. BASE DERIVED
  • 8. 2. MULTILEVEL INHERITANCE In this type of inheritance, the derived class becomes the base of another class. BASE 1 DERIVED BASE 2
  • 9. 3. MULTIPLE INHERITANCE BASE 1 DERIVED BASE 2 In this type of inheritance, the derived class inherits from one or more base classes.
  • 10. 4. HIERARCHICAL INHERITANCE In this type of inheritance, the base class is inherited by more than one class. BASE 1 DERIVED 2DERIVED 1
  • 11. 5. HYBRID INHERITANCE This inheritance is a combination of multiple, hierarchical and multilevel inheritance. BASE 1 BASE 2 DERIVED 2DERIVED 1
  • 13. EXAMPLES ON INHERITANCE Syntax: class subclass (super): For Example class person: def __init__(self,name,age): self.name=name self.age=age def getName(self): return self.name
  • 14. EXAMPLES ON INHERITANCE def getAge(self): return self.Age class student(person): def __init__(self,name,age,rollno,marks): super(student,self)._init_(self, name, age) self.rollno=rollno self.marks=marks def getRoll(self): return self.rollno def getMarks(self): return self.Marks
  • 15. EXAMPLES ON INHERITANCE The above example implements single inheritance. The class student extends the class person. The class student adds two instance variables rollno and marks. In order to add new instance variables, the _init_() method defined in class person needs to be extended. The __init__() function of subclass student initializesname and age attributes of superclass person and also create new attributes rollno and marks.
  • 17. EXTENDING __init__ () METHOD In python the above task of extending __init__ () can be achieved the following ways: i) By using super() function ii) By using name of the super class.
  • 19. super() FUNCTION In the above example, the class student and class person both have __init__ () method. The __init__ () method is defined in cIass person and extended in class student. In Python, super() function is used to call the methods of base class which have been extended in derived class Syntax: super(type, variable) bound object
  • 20. super() FUNCTION EXAMPLE class student(person): def __init__(self,name,age,rollno,marks): super(student, self).__init__ (self, name, age) self.rollno=rollno self.marks=marks
  • 21. BY USING NAME OF THE SUPER CLASS
  • 22. BY USING NAME OF THE SUPER CLASS As discussed above, the class student and class person both have __init__ () method. The __init__ () method is defined in cIass person and extended in class student. In Python, name of the base class can also be used to access the method of the base class which has been extended in derived class.
  • 23. BY USING NAME OF THE SUPER CLASS Example class student(person): def __init__(self,name,age,rollno,marks): person.__init__ (self, name, age) self.rollno=rollno self.marks=marks
  • 25. MULTIPLE INHERITANCE EXAMPLE Python supports a limited form of multiple inheritance as well. A class definition with multiple base classes looks like this class SubClassName( Base1, Base2, Base3): <statement1> . . . <statement N>
  • 26. MULTIPLE INHERITANCE EXAMPLE Python supports a limited form of multiple inheritance as well. A class definition with multiple base classes looks like this class SubClassName( Base1, Base2, Base3): <statement1> . . . <statement N>
  • 27. MULTIPLE INHERITANCE EXAMPLE For old-style classes, the only rule is depth-first, left-to-right. Thus, if an attribute is not found in SubClassName, it is searched in Base1, then (recursively) in the base classes of Base1, and only if it is not found there, it is searched in Base2, and so on
  • 28. MULTIPLE INHERITANCE EXAMPLE For Example: class student(object): def __init__(self,Id,name): self.Id=Id self.name=name def getName(self): return self.name def getId(self): return self.Id def show(self): print self.name
  • 29. MULTIPLE INHERITANCE EXAMPLE print self.Id class Teacher(): def __init__(self,tec_Id,tec_name, subject): self.tec_Id=tec_Id self.tec_name=tec_name self.subject=subject def getName(self): return self.tec_name def getId(self): return self.tec_Id
  • 30. MULTIPLE INHERITANCE EXAMPLE def getSubject(self): return self.ubject def show(self): print self. tec_name print self.tec_Id print self.subject class school(student,Teacher): def __init__(self, ID, name, tec_Id, tec_name, subject, sch_Id): student.__init__ (self,ID,name) Teacher. __init__(self,tec_Id,tec_name, subject) self.sch_Id= sch_Id
  • 31. MULTIPLE INHERITANCE EXAMPLE def getId(self ): return self.sch_Id def display(self): return self.sch_Id In above example class school inherits class student and teacher. Let us consider these outputs >>> s1=school(3,"Sham",56,"Ram","FIT",530) >>> s1.display() 530
  • 32. MULTIPLE INHERITANCE EXAMPLE >>> s1.getId() 530 >>> s1.show() Sham 3 The object of class school takes six instance variables. The first five instance variables have been defined in the base classes (school and Teacher). The sixth instance variable is defined in class school.
  • 33. MULTIPLE INHERITANCE EXAMPLE s1.display()displays the sch_Id and s1.getId() also returns the sch_id. The classes school and Teacher both have the method show (). But as shown in above, the objects1 of class school access the method of class student. This because of depth-first, left-to-right rule. Also, method getId()of class school is overriding the methods with same names in the base classes.
  • 35. The feature of overriding methods enables the programmer to provide specific implementation to a method in the subclass which is already implemented in the superclass. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. OVERRIDING METHODS
  • 36. Example 1: class student(person): def __init__(self,name,age,rollno,marks): super(student,self).__init__(self, name, age) self.rollno=rollno self.marks=marks def getRoll(self): return self.rollno def getMarks(self): return self.Marks OVERRIDING METHODS
  • 37. def show(self): print self.rollno print self.marks As shown in the above example class student inherits class person. Both the classes have a function show( ). The function show() in student is overriding function show() in person(). OVERRIDING METHODS
  • 38. The object of class person will print name and age. The object of class student will print rollno and marks. In case it is required that function show of class student should display name, age, rollno and marks, we should make the following change in class student class student(person): def __init__(self,name,age,rollno,marks): super(student,self).__init__(self,name,age) OVERRIDING METHODS
  • 39. self.rollno=rollno self.marks=marks def getRoll(self): return self.rollno def getMarks(self): return self.Marks def show(self): person.show( ) print self.rollno print self.marks OVERRIDING METHODS
  • 40. CLASS TEST 1. What is inheritance? 2. What are the types of inheritance? 3. Explain Multiple Inheritance with suitable example 4. What is super? Explain 5. Explain Overriding functions Class : XII Time: 40 Min Topic: Inheritance Max Marks: 20