SlideShare a Scribd company logo
Object	Oriented	Programming	in	
Python:	Inheritance
Aleksander	Fabijan
Today’s	Goals
1. Provide	an	introduction	to	inheritance	in	OOP.
• Why and	when should	we	inherit	from	other	objects?
• How do	we	inherit	from	objects	in	Python?
2. Provide	an	introduction	to	method	overriding.
From	Last	Time
From	Last	Time	(cnt.)
Comparing	Bus	&	Taxi
Comparing	Bus	&	Taxi
Classes	share	similar	variables
Inheritance
• Inheritance	simplifies	our	code	through	reuse	of	the	code	that	has	been	already	
written.
• Think	about	the	Taxi	and	Bus,	and	what	they	have	in	common.
• Inheritance	is	a	relation	between	a	parent	class	(e.g.	Vehicle)	and	children	classes	
(e.g.	Taxi,	Bus,	Truck,	etc.)
• A	class	inherits	attributes and	behavior methods	from	its	parent	classes.
Taxi
DriverName
NumberOfWheels
NumberOfSeats
OnDuty
Bus
DriverName
NumberOfWheels
NumberOfSeats
Color
Taxi
DriverName
NumberOfWheels
NumberOfSeats
OnDuty
Bus
DriverName
NumberOfWheels
NumberOfSeats
Color
Wait,	we	wrote	the	same	three	lines	of	code	in	both	classes?
There	must	be	a	better	way!!!
Vehicle
DriverName
NumberOfWheels
NumberOfSeats
Taxi
OnDuty
Bus
Color
We	moved	the	commons	in	a	parent	class
The	child	classes	only	keep	the	attributes	and	methods	relevant	to	them
OOP	Inheritance	in	Python
1. Create	a	parent	class	(e.g.	Vehicle)	with	the	common	attributes	and	
common	methods.
2. Create	child	classes	(e.g.	Bus	and	Taxi)	with	the	extended	attributes
and	extended	methods.
• Pass	the	class	definition	to	the	child	(e.g.	Class	Bus(Vehicle): …)
• Use the	parent attributes and	methods through super().
In	Python	code
In	Python	code
In	Python	code
In	Python	code
In	Python	code
Exercise	time!
Model	the	following	problem	in	Python	code:	
• Frida	Jacobsson is	a	student at	MAH.	Her	Skype	nickname	is	frida96.
• Aleksander	Fabijan	is	a	researcher at	MAH.		He	teaches	DA712	and	DA374.
• They	are	both	Humans.
Exercise	time!
Model	the	following	problem	in	Python	code:	
• Frida	Jacobsson is	a	Student at	MAH.	Her	Skype	nickname	is	frida96.
• Aleksander	is	a	Researcher at	MAH.		He	teaches	DA712	and	DA374.
• They	are	both	Humans.
Suggestion:	
1) Create	a	class	Human	that	initiates	a	new	human	with	a	name.	
2) Next,	create	two	classes	(e.g.	Student and	Researcher)	that	inherit	from	human,
3) Finally,	add	the	skype	nickname	and	the	list	of	courses	to	the	new	classes.
Code	snippets	for	help:
• class	Taxi(Vehicle):	 #creates	a	child	class	from	Human
• super().__init__(name,	lastname)						 #calls	the	parent’s	__init__method
LC
Method	Overriding
• Method	overriding	is	an	object-oriented	programming	feature	that	
allows	a	subclass	to	provide	a	different	implementation	of	a	method	
that	is	already	defined	by	its	superclass	or	by	one	of	its	superclasses.
• __init__	in	the	child	class	(e.g.	Taxi)	overrides	the	__init__	method	
from	the	parent	class.
Example	of	overriding	__str__
Let’s	add	a	__str__	method	that	nicely	prints	our	Vehicle	details	on	the	screen.	
Output:
Example	of	overriding	__str__
Example	of	overriding	__str__
Exercise	Time
Part1:		Update	your	class	Human	with	a	__str__	method	that	can	be	used	on	print.	It	
should	return	the	name	and	lastname of	the	human.	Try	it	out	by	creating	one	human	in	
code.
Part2:	Update	your	class	student	and	class	researcher by	overriding	the	__str__	method.
• __str__	in	the	child	classes	should	use	super().__str__(args)	to	call	its	parent	method	to	
print	out	the	name	and	lastname.
Part3:	For	student,	override	the	__str__(args)	method	so	it	returns	in	addition	to	the	name	
and	lastname,	also	the	skype	nickname.		Do	the	same	for	the	researcher	with	his	phone	
extension.
Takeaways
Today,	we	learned	how	and	when	to	use	inheritance	in	python	OOP.	
• Whenever	our	classes	can	reuse	the	attributes	and	methods	from	
parent	classes.
• We	inherit	from	parent	classes	by	passing	their	name	as	a	parameter	
to	our	child	class.
• We	reuse	the	methods	and	attributes	from	parent	classes	by	using	
super().
We	also	learned	how	to	override	methods	in	python.

More Related Content

What's hot (13)

PPT
Basic concepts of oops
Chandrakiran Satdeve
 
PPTX
2classes in c#
Sireesh K
 
PPTX
Object Oriented Concept
D Nayanathara
 
PDF
Introduction to object oriented programming
Abzetdin Adamov
 
PDF
Object Oriented Programming Concepts
246paa
 
PPT
OOP programming
anhdbh
 
KEY
Java Building Blocks
Cate Huston
 
PPTX
Object Oriented Technologies
Tushar B Kute
 
PPTX
OOPS features using Objective C
Tiyasi Acharya
 
PPT
Object-oriented concepts
BG Java EE Course
 
PPTX
CPP15 - Inheritance
Michael Heron
 
PDF
Farhaan Ahmed, BCA 2nd Year
dezyneecole
 
PPTX
L9 wrapper classes
teach4uin
 
Basic concepts of oops
Chandrakiran Satdeve
 
2classes in c#
Sireesh K
 
Object Oriented Concept
D Nayanathara
 
Introduction to object oriented programming
Abzetdin Adamov
 
Object Oriented Programming Concepts
246paa
 
OOP programming
anhdbh
 
Java Building Blocks
Cate Huston
 
Object Oriented Technologies
Tushar B Kute
 
OOPS features using Objective C
Tiyasi Acharya
 
Object-oriented concepts
BG Java EE Course
 
CPP15 - Inheritance
Michael Heron
 
Farhaan Ahmed, BCA 2nd Year
dezyneecole
 
L9 wrapper classes
teach4uin
 

Similar to Introduction to OOP in python inheritance (20)

PPTX
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
PDF
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
PPTX
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
PDF
All about python Inheritance.python codingdf
adipapai181023
 
PPTX
601109769-Pythondyttcjycvuv-Inheritance .pptx
srinivasa gowda
 
PPTX
arthimetic operator,classes,objects,instant
ssuser77162c
 
PDF
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
PPTX
Python programming computer science and engineering
IRAH34
 
PPT
inheritance in python with full detail.ppt
ssuser7b0a4d
 
PDF
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
PDF
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
PPTX
Class and Objects in python programming.pptx
Rajtherock
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PPTX
Problem solving with python programming OOP's Concept
rohitsharma24121
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPTX
OOP-part-2 object oriented programming.pptx
palmakyonna
 
PPTX
Object Oriented Programming -Single Inheritance.pptx
jospinjj
 
PDF
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
PPTX
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
All about python Inheritance.python codingdf
adipapai181023
 
601109769-Pythondyttcjycvuv-Inheritance .pptx
srinivasa gowda
 
arthimetic operator,classes,objects,instant
ssuser77162c
 
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Python programming computer science and engineering
IRAH34
 
inheritance in python with full detail.ppt
ssuser7b0a4d
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Class and Objects in python programming.pptx
Rajtherock
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Problem solving with python programming OOP's Concept
rohitsharma24121
 
OOP-part-2 object oriented programming.pptx
palmakyonna
 
Object Oriented Programming -Single Inheritance.pptx
jospinjj
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Ad

More from Aleksander Fabijan (7)

PPTX
Retrospective 1
Aleksander Fabijan
 
PPTX
Python oop third class
Aleksander Fabijan
 
PDF
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
PDF
Introduction to data visualisation with D3
Aleksander Fabijan
 
PDF
JavaScript development methodology
Aleksander Fabijan
 
PDF
Introduction to js (cnt.)
Aleksander Fabijan
 
PDF
Javascript intro for MAH
Aleksander Fabijan
 
Retrospective 1
Aleksander Fabijan
 
Python oop third class
Aleksander Fabijan
 
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
Introduction to data visualisation with D3
Aleksander Fabijan
 
JavaScript development methodology
Aleksander Fabijan
 
Introduction to js (cnt.)
Aleksander Fabijan
 
Javascript intro for MAH
Aleksander Fabijan
 
Ad

Recently uploaded (20)

PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PPTX
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 

Introduction to OOP in python inheritance