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)

PPTX
arthimetic operator,classes,objects,instant
ssuser77162c
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Access modifiers in Python
Santosh Verma
 
PPTX
Python: Access Control
Damian T. Gordon
 
PDF
Object oriented Programming via python.pdf
lal2sardar27991
 
PDF
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
PPTX
OOPS 46 slide Python concepts .pptx
mrsam3062
 
PPTX
Presentation - Copy no vaperpoit asd.pptx
RajshreePathir
 
PPTX
Presentation on Classes In Python Programming language
AsadKhokhar14
 
PPTX
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PDF
OOP in Python, a beginners guide..........
FerryKemperman
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Encapsulation C++ Piller of OOP it is the important piller
an7539661
 
PDF
Object-Oriented Programming System presentation
PavanKumarPathipati
 
PPTX
Presentation related to Encapsulation and OOPs
vinitsinghthakur46
 
PPTX
Presen5416846534653416354165341864adeadvdes
RajshreePathir
 
PPTX
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
RajshreePathir
 
arthimetic operator,classes,objects,instant
ssuser77162c
 
Object Oriented Programming in Python
Sujith Kumar
 
Access modifiers in Python
Santosh Verma
 
Python: Access Control
Damian T. Gordon
 
Object oriented Programming via python.pdf
lal2sardar27991
 
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
OOPS 46 slide Python concepts .pptx
mrsam3062
 
Presentation - Copy no vaperpoit asd.pptx
RajshreePathir
 
Presentation on Classes In Python Programming language
AsadKhokhar14
 
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
OOP in Python, a beginners guide..........
FerryKemperman
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Encapsulation C++ Piller of OOP it is the important piller
an7539661
 
Object-Oriented Programming System presentation
PavanKumarPathipati
 
Presentation related to Encapsulation and OOPs
vinitsinghthakur46
 
Presen5416846534653416354165341864adeadvdes
RajshreePathir
 
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
RajshreePathir
 

More from abigailjudith8 (12)

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

Recently uploaded (14)

DOCX
Nowgoal Prediction Palmeiras vs Botafogo 6.28.docx
nowgoalar7
 
DOCX
British and Irish Lions Farrell’s Leadership Born in Crisis.docx
eticketing
 
PDF
Gear Up: The Ultimate Lacrosse Equipment Checklist [Infographic]
Stringers Society Lacrosse
 
PPTX
football play 10 messi r9 ronaldinho.pptx
Gaurav Gupta
 
DOCX
Australia beats Japan to move closer to World Cup spot.docx
footballworldcuptick
 
DOCX
Assessing the Australia' Back Three Ahead of the Women Rugby World Cup.docx
World Creative Solutions
 
PDF
SEMIFINALES 2 LIGA MURO 2025 BASQUET.pdf
Arturo Pacheco Alvarez
 
DOCX
Nowgoal Prediction Benfica vs Chelsea 6.28.docx
nowgoalar7
 
PDF
Chad Readey - Passion For Sports And Data Science
Chad Readey
 
PDF
USRPT for WAFSU.org as presented by coach and author, Dave M
Mark Rauterkus
 
PDF
RallyFuel Empowering School Fundraising.pdf
RallyFuel
 
DOCX
British and Irish Lions Fall to Argentina in 2025 Tour Open.docx
eticketing
 
PPTX
secondary market fraud proof ticketing.pptx
counterten
 
PPTX
football player 1 messsi ronaldo nyymar .pptx
Gaurav Gupta
 
Nowgoal Prediction Palmeiras vs Botafogo 6.28.docx
nowgoalar7
 
British and Irish Lions Farrell’s Leadership Born in Crisis.docx
eticketing
 
Gear Up: The Ultimate Lacrosse Equipment Checklist [Infographic]
Stringers Society Lacrosse
 
football play 10 messi r9 ronaldinho.pptx
Gaurav Gupta
 
Australia beats Japan to move closer to World Cup spot.docx
footballworldcuptick
 
Assessing the Australia' Back Three Ahead of the Women Rugby World Cup.docx
World Creative Solutions
 
SEMIFINALES 2 LIGA MURO 2025 BASQUET.pdf
Arturo Pacheco Alvarez
 
Nowgoal Prediction Benfica vs Chelsea 6.28.docx
nowgoalar7
 
Chad Readey - Passion For Sports And Data Science
Chad Readey
 
USRPT for WAFSU.org as presented by coach and author, Dave M
Mark Rauterkus
 
RallyFuel Empowering School Fundraising.pdf
RallyFuel
 
British and Irish Lions Fall to Argentina in 2025 Tour Open.docx
eticketing
 
secondary market fraud proof ticketing.pptx
counterten
 
football player 1 messsi ronaldo nyymar .pptx
Gaurav Gupta
 
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.