SlideShare a Scribd company logo
Inheritance isthe capabilityof one classtoderive orinheritthe propertiesfromanotherclass.The benefitsof
inheritance are:
It representsreal-worldrelationshipswell.
It providesreusabilityof acode.We don’thave to write the same code againand again.Also,itallowsusto add
more featurestoa class withoutmodifyingit.
It istransitive innature,whichmeansthatif classB inheritsfromanotherclassA,thenall the subclassesof B would
automaticallyinheritfromclassA.
# A Pythonprogram to demonstrate inheritance
# Base or Superclass.Note objectinbracket.
# (Generally,objectismade ancestorof all classes)
# InPython3.x "classPerson"is
# equivalentto"classPerson(object)"
classPerson(object):
# Constructor
def __init__(self,name):
self.name=name
# To get name
def getName(self):
returnself.name
# To checkif thispersonisan employee
def isEmployee(self):
returnFalse
# InheritedorSubclass(Note Personinbracket)
classEmployee(Person):
# Here we returntrue
def isEmployee(self):
returnTrue
# Drivercode
emp= Person("Geek1") # AnObjectof Person
print(emp.getName(),emp.isEmployee())
emp= Employee("Geek2") #An Objectof Employee
print(emp.getName(),emp.isEmployee())
==============
Subclassing(Callingconstructorof parentclass)
A childclassneedstoidentifywhichclassisitsparentclass.Thiscan be done bymentioningthe parent classname in
the definitionof the childclass.
Eg: class subclass_name (superclass_name):
_ _ _
_ _ _
# Pythoncode to demonstrate howparentconstructors
# are called.
# parentclass
classPerson( object):
# __init__ isknownas the constructor
def __init__(self,name,idnumber):
self.name =name
self.idnumber=idnumber
def display(self):
print(self.name)
print(self.idnumber)
# childclass
classEmployee( Person):
def __init__(self,name,idnumber,salary,post):
self.salary=salary
self.post=post
# invokingthe __init__of the parentclass
Person.__init__(self,name,idnumber)
# creationof an objectvariable oran instance
a = Employee('Rahul',886012, 200000, "Intern")
# callingafunctionof the class Personusingitsinstance
a.display()
====
Differentformsof Inheritance:
1. Single inheritance:Whenachildclassinheritsfromonlyone parentclass,itiscalledsingle inheritance.We sawan
example above.
2. Multiple inheritance:Whenachildclassinheritsfrommultipleparentclasses,itiscalledmultiple inheritance.
Unlike Javaand like C++,Pythonsupportsmultipleinheritance.We specifyall parentclassesasacomma-separated
listinthe bracket.
# Pythonexampletoshowthe workingof multiple
# inheritance
classBase1(object):
def __init__(self):
self.str1="Geek1"
print("Base1")
classBase2(object):
def __init__(self):
self.str2="Geek2"
print("Base2")
classDerived(Base1,Base2):
def __init__(self):
# Callingconstructorsof Base1
# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")
def printStrs(self):
print(self.str1,self.str2)
ob = Derived()
ob.printStrs()
=======
3. Multilevel inheritance:Whenwe have achildand grandchild relationship.
# A Pythonprogramto demonstrate inheritance
# Base or Superclass.Note objectinbracket.
# (Generally,objectismade ancestorof all classes)
# InPython3.x "classPerson"is
# equivalentto"classPerson(object)"
classBase(object):
# Constructor
def __init__(self,name):
self.name=name
# To get name
def getName(self):
returnself.name
# InheritedorSubclass(Note Personinbracket)
classChild(Base):
# Constructor
def __init__(self,name,age):
Base.__init__(self,name)
self.age =age
# To get name
def getAge(self):
returnself.age
# InheritedorSubclass(Note Personinbracket)
classGrandChild(Child):
# Constructor
def __init__(self,name,age,address):
Child.__init__(self,name,age)
self.address=address
# To get address
def getAddress(self):
returnself.address
# Drivercode
g = GrandChild("Geek1",23,"Noida")
print(g.getName(), g.getAge(),g.getAddress())

More Related Content

What's hot (14)

Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
Dharmisha Sharma
 
C# note
C# noteC# note
C# note
Dr. Somnath Sinha
 
ICPC Demo
ICPC DemoICPC Demo
ICPC Demo
Patricia Deshane
 
Python master class part 1
Python master class part 1Python master class part 1
Python master class part 1
Chathuranga Bandara
 
polymorphism
polymorphism polymorphism
polymorphism
Imtiaz Hussain
 
PhD Dissertation
PhD DissertationPhD Dissertation
PhD Dissertation
Patricia Deshane
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 
Python master class 2
Python master class 2Python master class 2
Python master class 2
Chathuranga Bandara
 
Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기
Taehwan kwon
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
Suraj Bora
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
JDD2015: Frege - how to program with pure functions - Dierk König
JDD2015: Frege - how to program with pure functions - Dierk KönigJDD2015: Frege - how to program with pure functions - Dierk König
JDD2015: Frege - how to program with pure functions - Dierk König
PROIDEA
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
Dharmisha Sharma
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 
Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기Kotlin으로 안드로이드 개발하기
Kotlin으로 안드로이드 개발하기
Taehwan kwon
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
Suraj Bora
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
JDD2015: Frege - how to program with pure functions - Dierk König
JDD2015: Frege - how to program with pure functions - Dierk KönigJDD2015: Frege - how to program with pure functions - Dierk König
JDD2015: Frege - how to program with pure functions - Dierk König
PROIDEA
 

Similar to Python inheritance (20)

All about python Inheritance.python codingdf
All about python Inheritance.python codingdfAll about python Inheritance.python codingdf
All about python Inheritance.python codingdf
adipapai181023
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Inheritance
InheritanceInheritance
Inheritance
JayanthiNeelampalli
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Inheritance
InheritanceInheritance
Inheritance
Pradhan Rishi Sharma
 
Inheritance
InheritanceInheritance
Inheritance
Pradhan Rishi Sharma
 
Module 4 Effect of Reuse on using Inheritance.ppt
Module 4 Effect of Reuse on using Inheritance.pptModule 4 Effect of Reuse on using Inheritance.ppt
Module 4 Effect of Reuse on using Inheritance.ppt
ramlingams
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
Damian T. Gordon
 
python.pptx
python.pptxpython.pptx
python.pptx
Mvidhya9
 
All about python Inheritance.python codingdf
All about python Inheritance.python codingdfAll about python Inheritance.python codingdf
All about python Inheritance.python codingdf
adipapai181023
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Module 4 Effect of Reuse on using Inheritance.ppt
Module 4 Effect of Reuse on using Inheritance.pptModule 4 Effect of Reuse on using Inheritance.ppt
Module 4 Effect of Reuse on using Inheritance.ppt
ramlingams
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
python.pptx
python.pptxpython.pptx
python.pptx
Mvidhya9
 
Ad

Recently uploaded (20)

What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student NewsLDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptxChalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student NewsLDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Ad

Python inheritance

  • 1. Inheritance isthe capabilityof one classtoderive orinheritthe propertiesfromanotherclass.The benefitsof inheritance are: It representsreal-worldrelationshipswell. It providesreusabilityof acode.We don’thave to write the same code againand again.Also,itallowsusto add more featurestoa class withoutmodifyingit. It istransitive innature,whichmeansthatif classB inheritsfromanotherclassA,thenall the subclassesof B would automaticallyinheritfromclassA. # A Pythonprogram to demonstrate inheritance # Base or Superclass.Note objectinbracket. # (Generally,objectismade ancestorof all classes) # InPython3.x "classPerson"is # equivalentto"classPerson(object)" classPerson(object): # Constructor def __init__(self,name): self.name=name # To get name def getName(self): returnself.name # To checkif thispersonisan employee def isEmployee(self): returnFalse # InheritedorSubclass(Note Personinbracket) classEmployee(Person): # Here we returntrue def isEmployee(self): returnTrue # Drivercode emp= Person("Geek1") # AnObjectof Person print(emp.getName(),emp.isEmployee()) emp= Employee("Geek2") #An Objectof Employee print(emp.getName(),emp.isEmployee())
  • 2. ============== Subclassing(Callingconstructorof parentclass) A childclassneedstoidentifywhichclassisitsparentclass.Thiscan be done bymentioningthe parent classname in the definitionof the childclass. Eg: class subclass_name (superclass_name): _ _ _ _ _ _ # Pythoncode to demonstrate howparentconstructors # are called. # parentclass classPerson( object): # __init__ isknownas the constructor def __init__(self,name,idnumber): self.name =name self.idnumber=idnumber def display(self): print(self.name) print(self.idnumber) # childclass classEmployee( Person): def __init__(self,name,idnumber,salary,post): self.salary=salary self.post=post # invokingthe __init__of the parentclass Person.__init__(self,name,idnumber) # creationof an objectvariable oran instance a = Employee('Rahul',886012, 200000, "Intern") # callingafunctionof the class Personusingitsinstance a.display() ==== Differentformsof Inheritance: 1. Single inheritance:Whenachildclassinheritsfromonlyone parentclass,itiscalledsingle inheritance.We sawan example above.
  • 3. 2. Multiple inheritance:Whenachildclassinheritsfrommultipleparentclasses,itiscalledmultiple inheritance. Unlike Javaand like C++,Pythonsupportsmultipleinheritance.We specifyall parentclassesasacomma-separated listinthe bracket. # Pythonexampletoshowthe workingof multiple # inheritance classBase1(object): def __init__(self): self.str1="Geek1" print("Base1") classBase2(object): def __init__(self): self.str2="Geek2" print("Base2") classDerived(Base1,Base2): def __init__(self): # Callingconstructorsof Base1 # and Base2 classes Base1.__init__(self) Base2.__init__(self) print("Derived") def printStrs(self): print(self.str1,self.str2) ob = Derived() ob.printStrs() ======= 3. Multilevel inheritance:Whenwe have achildand grandchild relationship. # A Pythonprogramto demonstrate inheritance # Base or Superclass.Note objectinbracket. # (Generally,objectismade ancestorof all classes) # InPython3.x "classPerson"is # equivalentto"classPerson(object)" classBase(object): # Constructor
  • 4. def __init__(self,name): self.name=name # To get name def getName(self): returnself.name # InheritedorSubclass(Note Personinbracket) classChild(Base): # Constructor def __init__(self,name,age): Base.__init__(self,name) self.age =age # To get name def getAge(self): returnself.age # InheritedorSubclass(Note Personinbracket) classGrandChild(Child): # Constructor def __init__(self,name,age,address): Child.__init__(self,name,age) self.address=address # To get address def getAddress(self): returnself.address # Drivercode g = GrandChild("Geek1",23,"Noida") print(g.getName(), g.getAge(),g.getAddress())