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.