SlideShare a Scribd company logo
1 of 27
Download to read offline
Object Oriented
Programming Using
Python
1
1. Introduction to Object Oriented Programming in Python
2. Difference between object and procedural oriented
programming
3. What are Classes and Objects?
4. Object-Oriented Programming methodologies:
• Inheritance
• Polymorphism
• Encapsulation
• Abstraction
2
Index
3
1. Introduction to Object Oriented
Programming in Python
Object Oriented Programming is a way of
computer programming using the idea of
“objects” to represents data and methods. It is
also, an approach used for creating neat and
reusable code instead of a redundant one.
2. 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
5
class class1(): // class 1 is the name of the class
A class is a collection of objects or you can say it is a
blueprint of objects defining the common attributes and
behavior. Now the question arises, how do you do that?
Class is defined under a “Class” Keyword.
Example:
3. What are Classes and Objects?
6
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
self.id = id
emp1 = employee("harshit",22,1000,1234) //creating objects
emp2 = employee("arjun",23,2000,2234)
print(emp1.__dict__)//Prints dictionary
Creating an Object and Class in python:
Example:
7
❑ Inheritance
❑ Polymorphism
❑ Encapsulation
❑ Abstraction
4. Object-Oriented Programming
methodologies:
Inheritance:
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.
9
Single Inheritance:
Single level inheritance enables a derived class to inherit
characteristics from a single parent class.
class employee1()://This is a parent class
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
class childemployee(employee1)://This is a child class
def __init__(self, name, age, salary,id):
self.name = name
self.age = age
self.salary = salary
self.id = id
emp1 = employee1('harshit',22,1000)
print(emp1.age)
Example:
Output: 22
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.
class employee()://Super class
def __init__(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
class childemployee1(employee)://First child class
def __init__(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
Example:
class childemployee2(childemployee1)://Second child class
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
emp1 = employee('harshit',22,1000)
emp2 = childemployee1('arjun',23,2000)
print(emp1.age)
print(emp2.age)
Output: 22,23
Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived
class to inherit properties from a parent class.
class employee():
def __init__(self, name, age, salary): //Hierarchical Inheritance
self.name = name
self.age = age
self.salary = salary
Example:
class childemployee1(employee):
def __init__(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
class childemployee2(employee):
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
emp1 = employee('harshit',22,1000)
emp2 = employee('arjun',23,2000)
Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit
properties from more than one base class.
class employee1(): //Parent class
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
Example:
class employee2(): //Parent class
def __init__(self,name,age,salary,id):
self.name = name
self.age = age
self.salary = salary
self.id = id
class childemployee(employee1,employee2):
def __init__(self, name, age, salary,id):
self.name = name
self.age = age
self.salary = salary
self.id = id
emp1 = employee1('harshit',22,1000)
emp2 = employee2('arjun',23,2000,1234)
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.
19
20
Polymorphism is of two types:
❑ Compile-time Polymorphism
❑ Run-time Polymorphism
21
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”
22
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class employee2():
def name(self):
print("Rahul is his name")
def salary(self):
print("4000 is his salary")
def age(self):
print("23 is his age")
Example:
23
def func(obj)://Method Overloading
obj.name()
obj.salary()
obj.age()
obj_emp1 = employee1()
obj_emp2 = employee2()
func(obj_emp1)
func(obj_emp2)
Output:
Harshit is his name
3000 is his salary
22 is his age
Rahul is his name
4000 is his salary
23 is his age
24
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”.
25
class employee():
def __init__(self,name,age,id,salary):
self.name = name
self.age = age
self.salary = salary
self.id = id
def earn(self):
pass
class childemployee1(employee):
def earn(self): //Run-time polymorphism
print("no money")
Example:
26
class childemployee2(employee):
def earn(self):
print("has money")
c = childemployee1
c.earn(employee)
d = childemployee2
d.earn(employee)
Output: no money, has money
27
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.

More Related Content

Similar to Object Oriented Programming in Python Explained

C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
Basics of object oriented programming
Basics of object oriented programmingBasics of object oriented programming
Basics of object oriented programmingNitin Kumar Kashyap
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsSanmatiRM
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionSamuelAnsong6
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptxShuvrojitMajumder
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperLee Greffin
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismIt Academy
 
1669609053088_oops_final.pptx
1669609053088_oops_final.pptx1669609053088_oops_final.pptx
1669609053088_oops_final.pptxPandeeswariKannan
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfJP Chicano
 

Similar to Object Oriented Programming in Python Explained (20)

C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Lecture01
Lecture01Lecture01
Lecture01
 
SEMINAR
SEMINARSEMINAR
SEMINAR
 
Seminar
SeminarSeminar
Seminar
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Basics of object oriented programming
Basics of object oriented programmingBasics of object oriented programming
Basics of object oriented programming
 
Lecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad HaroonLecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad Haroon
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft Developer
 
OBJECT ORIENTED PROGRAMMING CONCEPTS IN C++.pptx
OBJECT ORIENTED PROGRAMMING CONCEPTS IN C++.pptxOBJECT ORIENTED PROGRAMMING CONCEPTS IN C++.pptx
OBJECT ORIENTED PROGRAMMING CONCEPTS IN C++.pptx
 
Object oriented programing
Object oriented programingObject oriented programing
Object oriented programing
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding Polymorphism
 
1669609053088_oops_final.pptx
1669609053088_oops_final.pptx1669609053088_oops_final.pptx
1669609053088_oops_final.pptx
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
java - oop's in depth journey
java - oop's in depth journeyjava - oop's in depth journey
java - oop's in depth journey
 

More from anaveenkumar4

Python Tutorial | Python Programming Language
Python Tutorial | Python Programming LanguagePython Tutorial | Python Programming Language
Python Tutorial | Python Programming Languageanaveenkumar4
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...anaveenkumar4
 
Electric circuits are classified in several ways. A direct-current circuit ca...
Electric circuits are classified in several ways. A direct-current circuit ca...Electric circuits are classified in several ways. A direct-current circuit ca...
Electric circuits are classified in several ways. A direct-current circuit ca...anaveenkumar4
 
MATLAB UNIT-III.pptx
MATLAB UNIT-III.pptxMATLAB UNIT-III.pptx
MATLAB UNIT-III.pptxanaveenkumar4
 
13012-41797-1-RV.pdf
13012-41797-1-RV.pdf13012-41797-1-RV.pdf
13012-41797-1-RV.pdfanaveenkumar4
 
1-s2.0-S1878029613000716-main.pdf
1-s2.0-S1878029613000716-main.pdf1-s2.0-S1878029613000716-main.pdf
1-s2.0-S1878029613000716-main.pdfanaveenkumar4
 
2_Off_Grid_Grid_Tied.pptx
2_Off_Grid_Grid_Tied.pptx2_Off_Grid_Grid_Tied.pptx
2_Off_Grid_Grid_Tied.pptxanaveenkumar4
 
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptxTRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptxanaveenkumar4
 

More from anaveenkumar4 (13)

Python Tutorial | Python Programming Language
Python Tutorial | Python Programming LanguagePython Tutorial | Python Programming Language
Python Tutorial | Python Programming Language
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
 
Electric circuits are classified in several ways. A direct-current circuit ca...
Electric circuits are classified in several ways. A direct-current circuit ca...Electric circuits are classified in several ways. A direct-current circuit ca...
Electric circuits are classified in several ways. A direct-current circuit ca...
 
MATLAB UNIT-III.pptx
MATLAB UNIT-III.pptxMATLAB UNIT-III.pptx
MATLAB UNIT-III.pptx
 
project.pptx
project.pptxproject.pptx
project.pptx
 
pump.ppt
pump.pptpump.ppt
pump.ppt
 
RET UNIT-4.pptx
RET UNIT-4.pptxRET UNIT-4.pptx
RET UNIT-4.pptx
 
13012-41797-1-RV.pdf
13012-41797-1-RV.pdf13012-41797-1-RV.pdf
13012-41797-1-RV.pdf
 
SolarThermal.pdf
SolarThermal.pdfSolarThermal.pdf
SolarThermal.pdf
 
1-s2.0-S1878029613000716-main.pdf
1-s2.0-S1878029613000716-main.pdf1-s2.0-S1878029613000716-main.pdf
1-s2.0-S1878029613000716-main.pdf
 
2_Off_Grid_Grid_Tied.pptx
2_Off_Grid_Grid_Tied.pptx2_Off_Grid_Grid_Tied.pptx
2_Off_Grid_Grid_Tied.pptx
 
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptxTRAINING PROGRAMME ON MATLAB  ASSOCIATE EXAM (1).pptx
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
 
Batch 6.pptx
Batch 6.pptxBatch 6.pptx
Batch 6.pptx
 

Recently uploaded

Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 

Recently uploaded (20)

Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
 

Object Oriented Programming in Python Explained

  • 2. 1. Introduction to Object Oriented Programming in Python 2. Difference between object and procedural oriented programming 3. What are Classes and Objects? 4. Object-Oriented Programming methodologies: • Inheritance • Polymorphism • Encapsulation • Abstraction 2 Index
  • 3. 3 1. Introduction to Object Oriented Programming in Python Object Oriented Programming is a way of computer programming using the idea of “objects” to represents data and methods. It is also, an approach used for creating neat and reusable code instead of a redundant one.
  • 4. 2. 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
  • 5. 5 class class1(): // class 1 is the name of the class A class is a collection of objects or you can say it is a blueprint of objects defining the common attributes and behavior. Now the question arises, how do you do that? Class is defined under a “Class” Keyword. Example: 3. What are Classes and Objects?
  • 6. 6 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 self.id = id emp1 = employee("harshit",22,1000,1234) //creating objects emp2 = employee("arjun",23,2000,2234) print(emp1.__dict__)//Prints dictionary Creating an Object and Class in python: Example:
  • 7. 7 ❑ Inheritance ❑ Polymorphism ❑ Encapsulation ❑ Abstraction 4. Object-Oriented Programming methodologies:
  • 8. Inheritance: 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.
  • 9. 9
  • 10. Single Inheritance: Single level inheritance enables a derived class to inherit characteristics from a single parent class.
  • 11. class employee1()://This is a parent class def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salary class childemployee(employee1)://This is a child class def __init__(self, name, age, salary,id): self.name = name self.age = age self.salary = salary self.id = id emp1 = employee1('harshit',22,1000) print(emp1.age) Example: Output: 22
  • 12. 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. class employee()://Super class def __init__(self,name,age,salary): self.name = name self.age = age self.salary = salary class childemployee1(employee)://First child class def __init__(self,name,age,salary): self.name = name self.age = age self.salary = salary Example:
  • 13. class childemployee2(childemployee1)://Second child class def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salary emp1 = employee('harshit',22,1000) emp2 = childemployee1('arjun',23,2000) print(emp1.age) print(emp2.age) Output: 22,23
  • 14. Hierarchical Inheritance: Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class. class employee(): def __init__(self, name, age, salary): //Hierarchical Inheritance self.name = name self.age = age self.salary = salary Example:
  • 15. class childemployee1(employee): def __init__(self,name,age,salary): self.name = name self.age = age self.salary = salary class childemployee2(employee): def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salary emp1 = employee('harshit',22,1000) emp2 = employee('arjun',23,2000)
  • 16. Multiple Inheritance: Multiple level inheritance enables one derived class to inherit properties from more than one base class. class employee1(): //Parent class def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salary Example:
  • 17. class employee2(): //Parent class def __init__(self,name,age,salary,id): self.name = name self.age = age self.salary = salary self.id = id class childemployee(employee1,employee2): def __init__(self, name, age, salary,id): self.name = name self.age = age self.salary = salary self.id = id emp1 = employee1('harshit',22,1000) emp2 = employee2('arjun',23,2000,1234)
  • 18. 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.
  • 19. 19
  • 20. 20 Polymorphism is of two types: ❑ Compile-time Polymorphism ❑ Run-time Polymorphism
  • 21. 21 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”
  • 22. 22 class employee1(): def name(self): print("Harshit is his name") def salary(self): print("3000 is his salary") def age(self): print("22 is his age") class employee2(): def name(self): print("Rahul is his name") def salary(self): print("4000 is his salary") def age(self): print("23 is his age") Example:
  • 23. 23 def func(obj)://Method Overloading obj.name() obj.salary() obj.age() obj_emp1 = employee1() obj_emp2 = employee2() func(obj_emp1) func(obj_emp2) Output: Harshit is his name 3000 is his salary 22 is his age Rahul is his name 4000 is his salary 23 is his age
  • 24. 24 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”.
  • 25. 25 class employee(): def __init__(self,name,age,id,salary): self.name = name self.age = age self.salary = salary self.id = id def earn(self): pass class childemployee1(employee): def earn(self): //Run-time polymorphism print("no money") Example:
  • 26. 26 class childemployee2(employee): def earn(self): print("has money") c = childemployee1 c.earn(employee) d = childemployee2 d.earn(employee) Output: no money, has money
  • 27. 27 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.