SlideShare a Scribd company logo
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Object Oriented Programming with Python
(Lecture # 02)
by
Muhammad Haroon
Object Oriented Programming with Python
In this Lecture, following aspects will be covered in detail:
✓ Difference between object and procedural oriented programming
✓ Object-Oriented Programming methodologies:
o Inheritance
o Polymorphism
o Encapsulation
o Abstraction
✓ Difference between Object-Oriented and Procedural Oriented Programming
Object-Oriented Programming (OOP) Procedural-Oriented Programming (POP)
It is a bottom-up approach It is a top-down approach
Program is divided into objects Program is divided into functions
Makes use of Access modifiers
‘public’, private’, protected’
Doesn’t use Access modifiers
It is more secure It is less secure
Object can move freely within member
functions
Data can move freely from function to function within
programs
It supports inheritance It does not support inheritance
Remember: “Creating an Object and Class in python” discuss in the previous lecture.
Creating an Object and Class in python
class employee():
def __init__(self,name,age,id,salary): #creating a function
self.name = name # self is an instance of a class
self.age = age
self.salary = salary
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
self.id = id
emp1 = employee("Muhammad Haroon",27,1000,1234) #creating objects
emp2 = employee("M.Haroon",27,2000,2234)
print(emp1.__dict__)#Prints dictionary
Figure 1: Creating an Object and Class in python
Explanation:
’emp1’ and ’emp2’ are the objects that are instantiated against the class ’employee’. Here, the word
(__dict__) is a “dictionary” which prints all the values of object ‘emp1’ against the given parameter
(name, age, salary). (__init__) acts like a constructor that is invoked whenever an object is created.
Object-Oriented Programming Methodologies:
Object-Oriented Programming methodologies deal with the following concepts:
✓ Inheritance
✓ Polymorphism
✓ Encapsulation
✓ Abstraction
Let us understand the first concept of inheritance in detail.
Inheritance
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Ever heard of this dialogue from relatives “you look exactly like your father/mother” the reason behind
this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of
characteristics from parent to child class without any modification”. The new class is called the
derived/child class and the one from which it is derived is called a parent/base class.
Let us understand each of the sub topics in detail.
Single Inheritance:
Single level inheritance enables a derived class to inherit characteristics from a single parent class.
Example:
Figure 2: Single Inheritance
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Explanation:
✓ I am taking the parent class and created a constructor (__init__), class itself is initializing the
attributes with parameters(‘name’, ‘age’ and ‘salary’).
✓ Created a child class ‘childemployee’ which is inheriting the properties from a parent class and
finally instantiated objects ’emp1′ and ’emp2′ against the parameters.
✓ Finally, I have printed the age of emp1. Well, you can do a hell lot of things like print the whole
dictionary or name or salary.
Multilevel Inheritance:
Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which
in turn inherits properties from his parent class.
Example:
Figure 3: Multilevel Inheritance:
Explanation:
✓ It is clearly explained in the code written above, Here I have defined the superclass as employee
and child class as childemployee1. Now, childemployee1 acts as a parent for childemployee2.
✓ I have instantiated two objects ’emp1′ and ’emp2′ where I am passing the parameters “name”,
“age”, “salary” for emp1 from superclass “employee” and “name”, “age, “salary” and “id” from
the parent class “childemployee1”.
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class.
Example:
Figure 4: Hierarchical Inheritance
Explanation:
✓ In the above Figure 4, you can clearly see there are two child class “childemployee1” and
“childemployee2”. They are inheriting functionalities from a common parent class that is
“employee”.
✓ Objects ’emp1′ and ’emp2′ are instantiated against the parameters ‘name’, ‘age’, ‘salary’.
Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit properties from more than one base class.
Example:
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Figure 5: Multiple Inheritance
Explanation:
✓ In the above Figure 5, I have taken two parent class “employee1” and “employee2”. And a child
class “childemployee”, which is inheriting both parent class by instantiating the objects ’emp1′
and ’emp2′ against the parameters of parent classes.
✓ This was all about inheritance, moving ahead in Object-Oriented Programming Python, let’s take
a deep dive in ‘polymorphism‘.
Polymorphism:
You all must have used GPS for navigating the route, Isn’t it amazing how many different routes you
come across for the same destination depending on the traffic, from a programming point of view this is
called ‘polymorphism’. It is one such OOP methodology where one task can be performed in several
different ways. To put it in simple words, it is a property of an object which allows it to take multiple
forms.
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Polymorphism is of two types:
✓ Compile-time Polymorphism
✓ Run-time Polymorphism
Compile-time Polymorphism:
A compile-time polymorphism also called as static polymorphism which gets resolved during the
compilation time of the program. One common example is “method overloading”. Let me show you a
quick example of the same.
Example:
Figure 6: Compile-time Polymorphism
Explanation:
✓ In the above Figure 6, I have created two classes ’employee1′ and ’employee2′ and created
functions for both ‘name’, ‘salary’ and ‘age’ and printed the value of the same without taking it
from the user.
✓ Now, welcome to the main part where I have created a function with ‘obj’ as the parameter and
calling all the three functions i.e. ‘name’, ‘age’ and ‘salary’.
✓ Later, instantiated objects emp_1 and emp_2 against the two classes and simply called the
function. Such type is called method overloading which allows a class to have more than one
method under the same name.
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Run-time Polymorphism:
A run-time Polymorphism is also, called as dynamic polymorphism where it gets resolved into the run
time. One common example of Run-time polymorphism is “method overriding”. Let me show you
through an example for a better understanding.
Example:
Figure 7: Run-time Polymorphism
Explanation:
In the above Figure 7, I have created two classes ‘childemployee1’ and ‘childemployee2’ which are
derived from the same base class ‘employee’. Here’s the catch one did not receive money whereas the
other one gets. Now the real question is how did this happen? Well, here if you look closely I created an
empty function and used Pass ( a statement which is used when you do not want to execute any command
or code). Now, Under the two derived classes, I used the same empty function and made use of the print
statement as ‘no money’ and ‘has money’. Lastly, created two objects and called the function.
Encapsulation:
In a raw form, encapsulation basically means binding up of data in a single class. Python does not have
any private keyword, unlike Java. A class shouldn’t be directly accessed but be prefixed in an underscore.
Let me show you an example for a better understanding.
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Example:
Figure 8: Encapsulation
Explanation:
You will get this question what is the underscore and error? Well, python class treats the private variables
as(__salary) which cannot be accessed directly.
So, I have made use of the setter method which provides indirect access to them in my next example.
Example:
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Figure 9: Encapsulation
Explanation:
Making Use of the setter method provides indirect access to the private class method. Here I have defined
a class employee and used a (__maxearn) which is the setter method used here to store the maximum
earning of the employee, and a setter function setmaxearn() which is taking price as the parameter.
This is a clear example of encapsulation where we are restricting the access to private class method and
then use the setter method to grant access.
Abstraction:
Suppose you booked a movie ticket from bookmyshow using net banking or any other process. You don’t
know the procedure of how the pin is generated or how the verification is done. This is called
‘abstraction’ from the programming aspect, it basically means you only show the implementation details
of a particular process and hide the details from the user. It is used to simplify complex problems by
modeling classes appropriate to the problem.
An abstract class cannot be instantiated which simply means you cannot create objects for this type of
class. It can only be used for inheriting the functionalities.
Example:
OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Figure 10: Abstraction
Explanation:
As you can see in the above Figure 10, we have imported an abstract method and the rest of the program
has a parent and a derived class. An object is instantiated for the ‘childemployee’ base class and
functionality of abstract is being used.
This brings us to the end of our Lecture on “OOP with Python”. I hope you have cleared with all the
concepts related to Python class, objects and object-oriented concepts in python. Make sure you practice
as much as possible and revert your experience.
End

More Related Content

What's hot (20)

PPTX
RL_chapter1_to_chapter4
hiroki yamaoka
 
PDF
木を綺麗に描画するアルゴリズム
mfumi
 
PDF
03 第3.6節-第3.8節 ROS2の基本機能(2/2)
Mori Ken
 
PPT
Queue Data Structure
Zidny Nafan
 
PDF
대학원 신입생을 위한 연구 길잡이
Woosung Kwon
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PDF
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
at grandpa
 
PPTX
Hashing And Hashing Tables
Chinmaya M. N
 
PPTX
劣モジュラ最適化と機械学習1章
Hakky St
 
PPTX
Arguments and methods of proof
Robert Geofroy
 
PPTX
UNIT-III-PPT.pptx
DakshBaveja
 
PPTX
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
PDF
オセロの終盤ソルバーを100倍以上高速化した話
京大 マイコンクラブ
 
PDF
Data Structures and Algorithm Analysis in C++ 4th edition Mark A. Weiss Solut...
javan012
 
PDF
Lecture19 unionsin c.ppt
eShikshak
 
PPTX
CVE-2021-3156 Baron samedit (sudoの脆弱性)
Tetsuya Hasegawa
 
PDF
OCaml-MPST / Global Protocol Combinators
Keigo Imai
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PDF
今日からできる構造学習(主に構造化パーセプトロンについて)
syou6162
 
PDF
Constructors and destructors
Prof. Dr. K. Adisesha
 
RL_chapter1_to_chapter4
hiroki yamaoka
 
木を綺麗に描画するアルゴリズム
mfumi
 
03 第3.6節-第3.8節 ROS2の基本機能(2/2)
Mori Ken
 
Queue Data Structure
Zidny Nafan
 
대학원 신입생을 위한 연구 길잡이
Woosung Kwon
 
Sparse matrix and its representation data structure
Vardhil Patel
 
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
at grandpa
 
Hashing And Hashing Tables
Chinmaya M. N
 
劣モジュラ最適化と機械学習1章
Hakky St
 
Arguments and methods of proof
Robert Geofroy
 
UNIT-III-PPT.pptx
DakshBaveja
 
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
オセロの終盤ソルバーを100倍以上高速化した話
京大 マイコンクラブ
 
Data Structures and Algorithm Analysis in C++ 4th edition Mark A. Weiss Solut...
javan012
 
Lecture19 unionsin c.ppt
eShikshak
 
CVE-2021-3156 Baron samedit (sudoの脆弱性)
Tetsuya Hasegawa
 
OCaml-MPST / Global Protocol Combinators
Keigo Imai
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
今日からできる構造学習(主に構造化パーセプトロンについて)
syou6162
 
Constructors and destructors
Prof. Dr. K. Adisesha
 

Similar to Lecture # 02 - OOP with Python Language by Muhammad Haroon (20)

PPTX
9-_Object_Oriented_Programming_Using_Python 1.pptx
Lahari42
 
PDF
Object Oriented programming Using Python.pdf
RishuRaj953240
 
PDF
9-_Object_Oriented_Programming_Using_Python.pdf
anaveenkumar4
 
PPTX
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
PPTX
Class and Objects in python programming.pptx
Rajtherock
 
PPTX
OOP concepts -in-Python programming language
SmritiSharma901052
 
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
PPTX
Introduction to OOP_Python_Lecture1.pptx
cpics
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPTX
Python advance
Mukul Kirti Verma
 
PPTX
OOPS 46 slide Python concepts .pptx
mrsam3062
 
PPTX
Problem solving with python programming OOP's Concept
rohitsharma24121
 
PPTX
Python programming computer science and engineering
IRAH34
 
PPTX
Inheritance
JayanthiNeelampalli
 
PPTX
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 
PPTX
arthimetic operator,classes,objects,instant
ssuser77162c
 
PPTX
OOP Concepts Python with code refrences.pptx
SofiMusic
 
PDF
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
PPTX
object oriented programming(PYTHON)
Jyoti shukla
 
9-_Object_Oriented_Programming_Using_Python 1.pptx
Lahari42
 
Object Oriented programming Using Python.pdf
RishuRaj953240
 
9-_Object_Oriented_Programming_Using_Python.pdf
anaveenkumar4
 
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
Class and Objects in python programming.pptx
Rajtherock
 
OOP concepts -in-Python programming language
SmritiSharma901052
 
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
Introduction to OOP_Python_Lecture1.pptx
cpics
 
Python advance
Mukul Kirti Verma
 
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Problem solving with python programming OOP's Concept
rohitsharma24121
 
Python programming computer science and engineering
IRAH34
 
Inheritance
JayanthiNeelampalli
 
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 
arthimetic operator,classes,objects,instant
ssuser77162c
 
OOP Concepts Python with code refrences.pptx
SofiMusic
 
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
object oriented programming(PYTHON)
Jyoti shukla
 
Ad

More from National College of Business Administration & Economics ( NCBA&E) (20)

PPTX
Lecturre 07 - Chapter 05 - Basic Communications Operations
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 06 - Chapter 4 - Communications in Networks
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture02 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture01 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 04 (Part 01) - Measure of Location
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 04 chapter 2 - Parallel Programming Platforms
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 04 Chapter 1 - Introduction to Parallel Computing
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Course outline of parallel and distributed computing
National College of Business Administration & Economics ( NCBA&E)
 
Lecturre 07 - Chapter 05 - Basic Communications Operations
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 06 - Chapter 4 - Communications in Networks
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
National College of Business Administration & Economics ( NCBA&E)
 
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture02 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
Lecture01 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 (Part 01) - Measure of Location
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 chapter 2 - Parallel Programming Platforms
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 Chapter 1 - Introduction to Parallel Computing
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
National College of Business Administration & Economics ( NCBA&E)
 
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
National College of Business Administration & Economics ( NCBA&E)
 
Course outline of parallel and distributed computing
National College of Business Administration & Economics ( NCBA&E)
 
Ad

Recently uploaded (20)

PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Horarios de distribución de agua en julio
pegazohn1978
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 

Lecture # 02 - OOP with Python Language by Muhammad Haroon

  • 1. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Object Oriented Programming with Python (Lecture # 02) by Muhammad Haroon Object Oriented Programming with Python In this Lecture, following aspects will be covered in detail: ✓ Difference between object and procedural oriented programming ✓ Object-Oriented Programming methodologies: o Inheritance o Polymorphism o Encapsulation o Abstraction ✓ Difference between Object-Oriented and Procedural Oriented Programming Object-Oriented Programming (OOP) Procedural-Oriented Programming (POP) It is a bottom-up approach It is a top-down approach Program is divided into objects Program is divided into functions Makes use of Access modifiers ‘public’, private’, protected’ Doesn’t use Access modifiers It is more secure It is less secure Object can move freely within member functions Data can move freely from function to function within programs It supports inheritance It does not support inheritance Remember: “Creating an Object and Class in python” discuss in the previous lecture. Creating an Object and Class in python class employee(): def __init__(self,name,age,id,salary): #creating a function self.name = name # self is an instance of a class self.age = age self.salary = salary
  • 2. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 self.id = id emp1 = employee("Muhammad Haroon",27,1000,1234) #creating objects emp2 = employee("M.Haroon",27,2000,2234) print(emp1.__dict__)#Prints dictionary Figure 1: Creating an Object and Class in python Explanation: ’emp1’ and ’emp2’ are the objects that are instantiated against the class ’employee’. Here, the word (__dict__) is a “dictionary” which prints all the values of object ‘emp1’ against the given parameter (name, age, salary). (__init__) acts like a constructor that is invoked whenever an object is created. Object-Oriented Programming Methodologies: Object-Oriented Programming methodologies deal with the following concepts: ✓ Inheritance ✓ Polymorphism ✓ Encapsulation ✓ Abstraction Let us understand the first concept of inheritance in detail. Inheritance
  • 3. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Ever heard of this dialogue from relatives “you look exactly like your father/mother” the reason behind this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class. Let us understand each of the sub topics in detail. Single Inheritance: Single level inheritance enables a derived class to inherit characteristics from a single parent class. Example: Figure 2: Single Inheritance
  • 4. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Explanation: ✓ I am taking the parent class and created a constructor (__init__), class itself is initializing the attributes with parameters(‘name’, ‘age’ and ‘salary’). ✓ Created a child class ‘childemployee’ which is inheriting the properties from a parent class and finally instantiated objects ’emp1′ and ’emp2′ against the parameters. ✓ Finally, I have printed the age of emp1. Well, you can do a hell lot of things like print the whole dictionary or name or salary. Multilevel Inheritance: Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class. Example: Figure 3: Multilevel Inheritance: Explanation: ✓ It is clearly explained in the code written above, Here I have defined the superclass as employee and child class as childemployee1. Now, childemployee1 acts as a parent for childemployee2. ✓ I have instantiated two objects ’emp1′ and ’emp2′ where I am passing the parameters “name”, “age”, “salary” for emp1 from superclass “employee” and “name”, “age, “salary” and “id” from the parent class “childemployee1”.
  • 5. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Hierarchical Inheritance: Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class. Example: Figure 4: Hierarchical Inheritance Explanation: ✓ In the above Figure 4, you can clearly see there are two child class “childemployee1” and “childemployee2”. They are inheriting functionalities from a common parent class that is “employee”. ✓ Objects ’emp1′ and ’emp2′ are instantiated against the parameters ‘name’, ‘age’, ‘salary’. Multiple Inheritance: Multiple level inheritance enables one derived class to inherit properties from more than one base class. Example:
  • 6. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Figure 5: Multiple Inheritance Explanation: ✓ In the above Figure 5, I have taken two parent class “employee1” and “employee2”. And a child class “childemployee”, which is inheriting both parent class by instantiating the objects ’emp1′ and ’emp2′ against the parameters of parent classes. ✓ This was all about inheritance, moving ahead in Object-Oriented Programming Python, let’s take a deep dive in ‘polymorphism‘. Polymorphism: You all must have used GPS for navigating the route, Isn’t it amazing how many different routes you come across for the same destination depending on the traffic, from a programming point of view this is called ‘polymorphism’. It is one such OOP methodology where one task can be performed in several different ways. To put it in simple words, it is a property of an object which allows it to take multiple forms.
  • 7. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Polymorphism is of two types: ✓ Compile-time Polymorphism ✓ Run-time Polymorphism Compile-time Polymorphism: A compile-time polymorphism also called as static polymorphism which gets resolved during the compilation time of the program. One common example is “method overloading”. Let me show you a quick example of the same. Example: Figure 6: Compile-time Polymorphism Explanation: ✓ In the above Figure 6, I have created two classes ’employee1′ and ’employee2′ and created functions for both ‘name’, ‘salary’ and ‘age’ and printed the value of the same without taking it from the user. ✓ Now, welcome to the main part where I have created a function with ‘obj’ as the parameter and calling all the three functions i.e. ‘name’, ‘age’ and ‘salary’. ✓ Later, instantiated objects emp_1 and emp_2 against the two classes and simply called the function. Such type is called method overloading which allows a class to have more than one method under the same name.
  • 8. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Run-time Polymorphism: A run-time Polymorphism is also, called as dynamic polymorphism where it gets resolved into the run time. One common example of Run-time polymorphism is “method overriding”. Let me show you through an example for a better understanding. Example: Figure 7: Run-time Polymorphism Explanation: In the above Figure 7, I have created two classes ‘childemployee1’ and ‘childemployee2’ which are derived from the same base class ‘employee’. Here’s the catch one did not receive money whereas the other one gets. Now the real question is how did this happen? Well, here if you look closely I created an empty function and used Pass ( a statement which is used when you do not want to execute any command or code). Now, Under the two derived classes, I used the same empty function and made use of the print statement as ‘no money’ and ‘has money’. Lastly, created two objects and called the function. Encapsulation: In a raw form, encapsulation basically means binding up of data in a single class. Python does not have any private keyword, unlike Java. A class shouldn’t be directly accessed but be prefixed in an underscore. Let me show you an example for a better understanding.
  • 9. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Example: Figure 8: Encapsulation Explanation: You will get this question what is the underscore and error? Well, python class treats the private variables as(__salary) which cannot be accessed directly. So, I have made use of the setter method which provides indirect access to them in my next example. Example:
  • 10. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Figure 9: Encapsulation Explanation: Making Use of the setter method provides indirect access to the private class method. Here I have defined a class employee and used a (__maxearn) which is the setter method used here to store the maximum earning of the employee, and a setter function setmaxearn() which is taking price as the parameter. This is a clear example of encapsulation where we are restricting the access to private class method and then use the setter method to grant access. Abstraction: Suppose you booked a movie ticket from bookmyshow using net banking or any other process. You don’t know the procedure of how the pin is generated or how the verification is done. This is called ‘abstraction’ from the programming aspect, it basically means you only show the implementation details of a particular process and hide the details from the user. It is used to simplify complex problems by modeling classes appropriate to the problem. An abstract class cannot be instantiated which simply means you cannot create objects for this type of class. It can only be used for inheriting the functionalities. Example:
  • 11. OOP with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Figure 10: Abstraction Explanation: As you can see in the above Figure 10, we have imported an abstract method and the rest of the program has a parent and a derived class. An object is instantiated for the ‘childemployee’ base class and functionality of abstract is being used. This brings us to the end of our Lecture on “OOP with Python”. I hope you have cleared with all the concepts related to Python class, objects and object-oriented concepts in python. Make sure you practice as much as possible and revert your experience. End