SlideShare a Scribd company logo
Encapsulation in Python
Understanding Public, Protected, and
Private Attributes with Examples
What is Encapsulation?
• Encapsulation in Python is an object-oriented programming (OOP)
concept that refers to the bundling of data (attributes) and
methods (functions) that operate on the data into a single unit, or
class.
• Encapsulation is one of the key concepts of Object-Oriented
Programming (OOP). It restricts direct access to some of an object's
components, which prevents accidental modification of data.
• Encapsulation helps in:
• - Data hiding
• - Restricting access to internal object details
• - Controlling modifications to attributes
Public Attributes
• Public attributes are accessible from anywhere inside or
outside the class.
Example:
class Car:
def __init__(self, brand):
self.brand = brand # Public attribute
car1 = Car('Toyota')
print(car1.brand) # Output: Toyota
Protected Attributes
• Protected attributes are prefixed with a single underscore (_), indicating
that they should not be accessed directly outside the class but can still
be accessed.
Example:
class Car:
def __init__(self, brand):
self._brand = brand # Protected attribute
car1 = Car('Toyota')
print(car1._brand) # Output: Toyota (not recommended to access directly)
Private Attributes
Private attributes are prefixed with double underscores (__),
making them inaccessible directly outside the class.
Example:
class Car:
def __init__(self, brand):
self.__brand = brand # Private attribute
car1 = Car('Toyota')
print(car1.__brand) # Error: AttributeError
Accessing Private Attributes
• Private attributes can be accessed using getter methods.
Example:
class Car:
def __init__(self, brand):
self.__brand = brand
def get_brand(self):
return self.__brand
car1 = Car('Toyota')
print(car1.get_brand()) # Output: Toyota
Modifying Private Attributes
• Private attributes can be modified using setter methods.
Setter Method (set_brand): This new method provides a controlled way to modify
the __brand attribute.
class Car:
def __init__(self, brand):
self.__brand = brand # Private attribute initialized in the constructor
def get_brand(self):
return self.__brand # Getter method to access the private attribute
def set_brand(self, brand):
self.__brand = brand
car1 = Car('Toyota')
print(car1.get_brand()) # Output: Toyota
car1.set_brand('Honda')
print(car1.get_brand()) # Output: Honda
Summary
- Public attributes can be accessed from anywhere.
- Protected attributes are meant to be accessed
only within subclasses.
- Private attributes cannot be accessed directly but
can be accessed via methods.
• Encapsulation helps in maintaining security and
integrity of data in Python classes.
Protected Attributes
• Protected attributes are prefixed with a single underscore (_), indicating
that they should not be accessed directly outside the class but can still
be accessed.
Example:
class Car:
def __init__(self, brand):
self._brand = brand # Protected attribute
car1 = Car('Toyota')
print(car1._brand) # Output: Toyota (not recommended to access directly)
Protected Attributes
• Protected attributes are prefixed with a single underscore (_), indicating
that they should not be accessed directly outside the class but can still
be accessed.
Example:
class Car:
def __init__(self, brand):
self._brand = brand # Protected attribute
car1 = Car('Toyota')
print(car1._brand) # Output: Toyota (not recommended to access directly)
Protected Attributes
# Base class
class Animal:
def __init__(self, age):
self._age = age # Protected attribute
# Derived class
class Dog(Animal):
def __init__(self, age):
super().__init__(age)
def print_age(self):
# Accessing the protected attribute within a subclass
print(f"The dog's age is: {self._age}")
# Creating an instance of Dog
my_dog = Dog(5)
my_dog.print_age() # This will output: The dog's age is: 5
# Direct access from outside the class (not recommended, but possible)
print(my_dog._age) # Outputs 5, demonstrating that 'protected' is a convention
Summary
Protected: Accessible within the class and its
subclasses. It’s a guideline (or enforced by the
language) for controlled access.
Private: Strictly hidden inside the class, ensuring
that no external code or subclasses directly
modify sensitive data.

More Related Content

Similar to Encapsulation_Python_Programming_Language (20)

arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
Access modifiers in Python
Access modifiers in PythonAccess modifiers in Python
Access modifiers in Python
Santosh Verma
 
Python: Access Control
Python: Access ControlPython: Access Control
Python: Access Control
Damian T. Gordon
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
Kongunadu College of Engineering and Technology
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Presentation - Copy no vaperpoit asd.pptx
Presentation - Copy no vaperpoit asd.pptxPresentation - Copy no vaperpoit asd.pptx
Presentation - Copy no vaperpoit asd.pptx
RajshreePathir
 
Presentation on Classes In Python Programming language
Presentation on Classes In Python Programming languagePresentation on Classes In Python Programming language
Presentation on Classes In Python Programming language
AsadKhokhar14
 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHONUNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
FerryKemperman
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Encapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important pillerEncapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important piller
an7539661
 
Object-Oriented Programming System presentation
Object-Oriented Programming System presentationObject-Oriented Programming System presentation
Object-Oriented Programming System presentation
PavanKumarPathipati
 
Presentation related to Encapsulation and OOPs
Presentation related to Encapsulation and OOPsPresentation related to Encapsulation and OOPs
Presentation related to Encapsulation and OOPs
vinitsinghthakur46
 
Presen5416846534653416354165341864adeadvdes
Presen5416846534653416354165341864adeadvdesPresen5416846534653416354165341864adeadvdes
Presen5416846534653416354165341864adeadvdes
RajshreePathir
 
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptxPrese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
RajshreePathir
 
Object-Oriented Programming (OO) in pythonP) in python.pptx
Object-Oriented Programming (OO)  in pythonP)  in python.pptxObject-Oriented Programming (OO)  in pythonP)  in python.pptx
Object-Oriented Programming (OO) in pythonP) in python.pptx
institute of Geoinformatics and Earth Observation at PMAS ARID Agriculture University of Rawalpindi
 
oopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptxoopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
Access modifiers in Python
Access modifiers in PythonAccess modifiers in Python
Access modifiers in Python
Santosh Verma
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
OOPS 46 slide Python concepts .pptx
OOPS 46 slide Python concepts       .pptxOOPS 46 slide Python concepts       .pptx
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Presentation - Copy no vaperpoit asd.pptx
Presentation - Copy no vaperpoit asd.pptxPresentation - Copy no vaperpoit asd.pptx
Presentation - Copy no vaperpoit asd.pptx
RajshreePathir
 
Presentation on Classes In Python Programming language
Presentation on Classes In Python Programming languagePresentation on Classes In Python Programming language
Presentation on Classes In Python Programming language
AsadKhokhar14
 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHONUNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
FerryKemperman
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Encapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important pillerEncapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important piller
an7539661
 
Object-Oriented Programming System presentation
Object-Oriented Programming System presentationObject-Oriented Programming System presentation
Object-Oriented Programming System presentation
PavanKumarPathipati
 
Presentation related to Encapsulation and OOPs
Presentation related to Encapsulation and OOPsPresentation related to Encapsulation and OOPs
Presentation related to Encapsulation and OOPs
vinitsinghthakur46
 
Presen5416846534653416354165341864adeadvdes
Presen5416846534653416354165341864adeadvdesPresen5416846534653416354165341864adeadvdes
Presen5416846534653416354165341864adeadvdes
RajshreePathir
 
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptxPrese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
RajshreePathir
 
oopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptxoopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
 

More from abigailjudith8 (12)

Endpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and VulnerabilitiesEndpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
Endpoint Security - Network Security InfrastructureEndpoint Security - Network Security Infrastructure
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
Polymorphism_in_Python_Programming_Language
Polymorphism_in_Python_Programming_LanguagePolymorphism_in_Python_Programming_Language
Polymorphism_in_Python_Programming_Language
abigailjudith8
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Application and Data security and Privacy.pptx
Application and Data security and Privacy.pptxApplication and Data security and Privacy.pptx
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptxCyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.pptSVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERSMACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.pptlect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
SVM introduction for machine learning engineersSVM introduction for machine learning engineers
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.pptBig Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptxINTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Endpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and VulnerabilitiesEndpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
Endpoint Security - Network Security InfrastructureEndpoint Security - Network Security Infrastructure
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
Polymorphism_in_Python_Programming_Language
Polymorphism_in_Python_Programming_LanguagePolymorphism_in_Python_Programming_Language
Polymorphism_in_Python_Programming_Language
abigailjudith8
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Application and Data security and Privacy.pptx
Application and Data security and Privacy.pptxApplication and Data security and Privacy.pptx
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptxCyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.pptSVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERSMACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.pptlect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
SVM introduction for machine learning engineersSVM introduction for machine learning engineers
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.pptBig Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptxINTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Ad

Recently uploaded (7)

Best Ticket Resale Verification System.pptx
Best Ticket Resale Verification System.pptxBest Ticket Resale Verification System.pptx
Best Ticket Resale Verification System.pptx
counterten
 
Team Work Presentation.pptx.pptx.pptx.pptx
Team Work Presentation.pptx.pptx.pptx.pptxTeam Work Presentation.pptx.pptx.pptx.pptx
Team Work Presentation.pptx.pptx.pptx.pptx
kayaniawais280
 
Management of sports with types of tournament.pptx
Management of sports with types of tournament.pptxManagement of sports with types of tournament.pptx
Management of sports with types of tournament.pptx
ZeusOp1
 
Football - IFAB - Laws of the Game 2025_26.pdf
Football - IFAB - Laws of the Game 2025_26.pdfFootball - IFAB - Laws of the Game 2025_26.pdf
Football - IFAB - Laws of the Game 2025_26.pdf
serdaraker1
 
Chess Mastery: The Path to Excellence (presentation)
Chess Mastery: The Path to Excellence (presentation)Chess Mastery: The Path to Excellence (presentation)
Chess Mastery: The Path to Excellence (presentation)
Brian Shechtman
 
NCAA Football Betting System That Will Change Your Gambling Life
NCAA Football Betting System That Will Change Your Gambling LifeNCAA Football Betting System That Will Change Your Gambling Life
NCAA Football Betting System That Will Change Your Gambling Life
Joe Duffy
 
Best Ticket Resale Verification System.pdf
Best Ticket Resale Verification System.pdfBest Ticket Resale Verification System.pdf
Best Ticket Resale Verification System.pdf
counterten
 
Best Ticket Resale Verification System.pptx
Best Ticket Resale Verification System.pptxBest Ticket Resale Verification System.pptx
Best Ticket Resale Verification System.pptx
counterten
 
Team Work Presentation.pptx.pptx.pptx.pptx
Team Work Presentation.pptx.pptx.pptx.pptxTeam Work Presentation.pptx.pptx.pptx.pptx
Team Work Presentation.pptx.pptx.pptx.pptx
kayaniawais280
 
Management of sports with types of tournament.pptx
Management of sports with types of tournament.pptxManagement of sports with types of tournament.pptx
Management of sports with types of tournament.pptx
ZeusOp1
 
Football - IFAB - Laws of the Game 2025_26.pdf
Football - IFAB - Laws of the Game 2025_26.pdfFootball - IFAB - Laws of the Game 2025_26.pdf
Football - IFAB - Laws of the Game 2025_26.pdf
serdaraker1
 
Chess Mastery: The Path to Excellence (presentation)
Chess Mastery: The Path to Excellence (presentation)Chess Mastery: The Path to Excellence (presentation)
Chess Mastery: The Path to Excellence (presentation)
Brian Shechtman
 
NCAA Football Betting System That Will Change Your Gambling Life
NCAA Football Betting System That Will Change Your Gambling LifeNCAA Football Betting System That Will Change Your Gambling Life
NCAA Football Betting System That Will Change Your Gambling Life
Joe Duffy
 
Best Ticket Resale Verification System.pdf
Best Ticket Resale Verification System.pdfBest Ticket Resale Verification System.pdf
Best Ticket Resale Verification System.pdf
counterten
 
Ad

Encapsulation_Python_Programming_Language

  • 1. Encapsulation in Python Understanding Public, Protected, and Private Attributes with Examples
  • 2. What is Encapsulation? • Encapsulation in Python is an object-oriented programming (OOP) concept that refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, or class. • Encapsulation is one of the key concepts of Object-Oriented Programming (OOP). It restricts direct access to some of an object's components, which prevents accidental modification of data. • Encapsulation helps in: • - Data hiding • - Restricting access to internal object details • - Controlling modifications to attributes
  • 3. Public Attributes • Public attributes are accessible from anywhere inside or outside the class. Example: class Car: def __init__(self, brand): self.brand = brand # Public attribute car1 = Car('Toyota') print(car1.brand) # Output: Toyota
  • 4. Protected Attributes • Protected attributes are prefixed with a single underscore (_), indicating that they should not be accessed directly outside the class but can still be accessed. Example: class Car: def __init__(self, brand): self._brand = brand # Protected attribute car1 = Car('Toyota') print(car1._brand) # Output: Toyota (not recommended to access directly)
  • 5. Private Attributes Private attributes are prefixed with double underscores (__), making them inaccessible directly outside the class. Example: class Car: def __init__(self, brand): self.__brand = brand # Private attribute car1 = Car('Toyota') print(car1.__brand) # Error: AttributeError
  • 6. Accessing Private Attributes • Private attributes can be accessed using getter methods. Example: class Car: def __init__(self, brand): self.__brand = brand def get_brand(self): return self.__brand car1 = Car('Toyota') print(car1.get_brand()) # Output: Toyota
  • 7. Modifying Private Attributes • Private attributes can be modified using setter methods. Setter Method (set_brand): This new method provides a controlled way to modify the __brand attribute. class Car: def __init__(self, brand): self.__brand = brand # Private attribute initialized in the constructor def get_brand(self): return self.__brand # Getter method to access the private attribute def set_brand(self, brand): self.__brand = brand car1 = Car('Toyota') print(car1.get_brand()) # Output: Toyota car1.set_brand('Honda') print(car1.get_brand()) # Output: Honda
  • 8. Summary - Public attributes can be accessed from anywhere. - Protected attributes are meant to be accessed only within subclasses. - Private attributes cannot be accessed directly but can be accessed via methods. • Encapsulation helps in maintaining security and integrity of data in Python classes.
  • 9. Protected Attributes • Protected attributes are prefixed with a single underscore (_), indicating that they should not be accessed directly outside the class but can still be accessed. Example: class Car: def __init__(self, brand): self._brand = brand # Protected attribute car1 = Car('Toyota') print(car1._brand) # Output: Toyota (not recommended to access directly)
  • 10. Protected Attributes • Protected attributes are prefixed with a single underscore (_), indicating that they should not be accessed directly outside the class but can still be accessed. Example: class Car: def __init__(self, brand): self._brand = brand # Protected attribute car1 = Car('Toyota') print(car1._brand) # Output: Toyota (not recommended to access directly)
  • 11. Protected Attributes # Base class class Animal: def __init__(self, age): self._age = age # Protected attribute # Derived class class Dog(Animal): def __init__(self, age): super().__init__(age) def print_age(self): # Accessing the protected attribute within a subclass print(f"The dog's age is: {self._age}") # Creating an instance of Dog my_dog = Dog(5) my_dog.print_age() # This will output: The dog's age is: 5 # Direct access from outside the class (not recommended, but possible) print(my_dog._age) # Outputs 5, demonstrating that 'protected' is a convention
  • 12. Summary Protected: Accessible within the class and its subclasses. It’s a guideline (or enforced by the language) for controlled access. Private: Strictly hidden inside the class, ensuring that no external code or subclasses directly modify sensitive data.