python

Encapsulation(oop)


Encapsulation is about bundling data (attributes) and methods (functions that operate on the data) into a single unit, called a class. This unit keeps the data safe from outside interference and misuse.

In simple word we can say Encapsulation in programming is like putting something in a box with clear instructions on how to use it without needing to know what's inside.

 

 

Key Concepts of Encapsulation

  1. Data Hiding: Encapsulation hides the internal state of the object from the outside world. Access to the internal state is restricted to the object's methods.
  2. Public, Protected, and Private Members: These are the different levels of access control for class members. 
  • Public Members: Accessible from anywhere. 
  • Protected Members: Accessible within the class and its subclasses (conventionally indicated by a single underscore ‘_’ prefix).
  • Private Members: Accessible only within the class (indicated by a double underscore'__' prefix).

 

 

Implementing Encapsulation in Python

Python uses naming conventions to indicate the level of access control for class members.

 

Public Members

Public members are accessible from anywhere. In Python, all class members are public by default.

Example:

class Car:
   def __init__(self, make, model):
       self.make = make  # Public attribute
       self.model = model  # Public attribute
   def display_info(self):
       return f"Car make: {self.make}, Model: {self.model}"
# Creating an instance of Car
car = Car("Toyota", "Camry")
print(car.display_info())  # Output: Car make: Toyota, Model: Camry
print(car.make)  # Output: Toyota
print(car.model)  # Output: Camry
 

 

Protected Members

Protected members are intended to be accessed only within the class and its subclasses. In Python, protected members are indicated by a single underscore ‘_’ prefix.

Example:

class Car:
   def __init__(self, make, model):
       self._make = make  # Protected attribute
       self._model = model  # Protected attribute
   def display_info(self):
       return f"Car make: {self._make}, Model: {self._model}"
# Creating an instance of Car
car = Car("Toyota", "Camry")
print(car.display_info())  # Output: Car make: Toyota, Model: Camry
print(car._make)  # Output: Toyota
print(car._model)  # Output: Camry
 

 

Private Members

Private members are intended to be accessed only within the class. In Python, private members are indicated by a double underscore ‘__’ prefix.

Example:

class Car:
   def __init__(self, make, model):
       self.__make = make  # Private attribute
       self.__model = model  # Private attribute
   def display_info(self):
       return f"Car make: {self.__make}, Model: {self.__model}"
# Creating an instance of Car
car = Car("Toyota", "Camry")
print(car.display_info())  # Output: Car make: Toyota, Model: Camry
# Accessing private members directly will result in an error
# print(car.__make)  # AttributeError: 'Car' object has no attribute '__make'
# print(car.__model)  # AttributeError: 'Car' object has no attribute '__model'
 

 

Accessing Private Members

Private members can be accessed through name mangling, where the member name is prefixed with _ClassName. However, this is not recommended as it breaks the encapsulation principle.

Example:

print(car._Car__make)  # Output: Toyota
print(car._Car__model)  # Output: Camry
 

 

Benefits of Encapsulation

  • Improves Security: Restricts unauthorized access and modification of data.
  • Increases Flexibility: Internal details can be changed without affecting other parts of the code.
  • Enhances Maintainability: Clear separation between an object's internal state and its external interface makes the code easier to maintain.
  • Promotes Modularity: Objects can be treated as black boxes, making it easier to understand and work with them.

 


python