SlideShare una empresa de Scribd logo
1 de 41
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

Más contenido relacionado

La actualidad más candente

Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python TutorialSimplilearn
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming Raghunath A
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 

La actualidad más candente (20)

Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 

Similar a Chapter 07 inheritance

06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eGina Bullock
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eGina Bullock
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javachauhankapil
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxsantoshkumar811204
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxNITHISG1
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptRithwikRanjan
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oopMuskanNazeer
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxnaeemcse
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdfkumari36
 

Similar a Chapter 07 inheritance (20)

Ruby object model
Ruby object modelRuby object model
Ruby object model
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
Java
JavaJava
Java
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 

Más de Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programmingPraveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with pythonPraveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingPraveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow chartsPraveen M Jigajinni
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingPraveen M Jigajinni
 

Más de Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computing
 
Chapter 2 operating systems
Chapter 2 operating systemsChapter 2 operating systems
Chapter 2 operating systems
 
Chapter 1 computer fundamentals
Chapter 1 computer  fundamentalsChapter 1 computer  fundamentals
Chapter 1 computer fundamentals
 

Último

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

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