What is Encapsulation?


Encapsulation is a principle of object-oriented programming that refers to the bundling of data and methods that operate on that data within one unit, or object. It is one of the four fundamental OOP concepts, along with inheritance, polymorphism, and abstraction.

The main benefit of encapsulation is that it allows you to hide the implementation details of a class from other parts of your code. This helps to reduce complexity and increase the maintainability of your code.

Here is an example of encapsulation in Python:

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # private attribute

    def get_balance(self):
        return self.__balance  # public method

    def set_balance(self, balance):
        if balance < 0:
            raise ValueError("balance cannot be negative")
        self.__balance = balance

In this example, the __balance attribute is a private attribute, meaning that it can only be accessed or modified from within the BankAccount class. The get_balance and set_balance methods are public methods, meaning that they can be called from outside the class.

This allows the user of the BankAccount class to manipulate the balance of the account using the public methods, without knowing the implementation details of how the balance is stored or validated.

Here are a few more examples of encapsulation in Python:

class Car:
    def __init__(self, make, model, year):
        self.__make = make
        self.__model = model
        self.__year = year
    def get_make(self):
        return self.__make
    def set_make(self, make):
        self.__make = make
    def get_model(self):
        return self.__model
    def set_model(self, model):
        self.__model = model
    def get_year(self):
        return self.__year
    def set_year(self, year):
        self.__year = year

class Employee:
    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary
    def get_name(self):
        return self.__name
    def set_name(self, name):
        self.__name = name
    def get_salary(self):
        return self.__salary
    def set_salary(self, salary):
        if salary < 0:
            raise ValueError("salary cannot be negative")
        self.__salary = salary

class Person:
    def __init__(self, name, age):
        self.__name = name
        self.__age = age
    def get_name(self):
        return self.__name
    def set_name(self, name):
        self.__name = name
    def get_age(self):
        return self.__age
    def set_age(self, age):
        if age < 0:
            raise ValueError("age cannot be negative")
        self.__age = age

In these examples, the data attributes (e.g., make, model, year, name, salary, age) are private, and can only be accessed or modified using the public methods (e.g., get_make, set_make, get_model, etc.). This allows the implementation details of the class to be hidden from the user.

Here are a few examples of encapsulation in real life:

  1. A television: The buttons on the television’s remote control allow you to change the channel, adjust the volume, and turn the TV on or off, without knowing the internal workings of the television.
  2. A car’s transmission: The gears and other components of a car’s transmission are enclosed in a sealed unit, and the driver can change gears using the gearshift lever without knowing the details of how the gears are connected or how they work.
  3. A thermostat: A thermostat allows you to set the desired temperature for your home, and it will automatically turn the heating or cooling system on or off to maintain that temperature. The internal sensors and mechanisms that control the temperature are hidden from the user.
  4. A cell phone: A cell phone has many complex components, such as the processor, memory, and display, but the user is only able to interact with it through the buttons, touchscreen, and other interface elements.
  5. A vending machine: A vending machine allows you to purchase items by inserting coins or bills and pressing buttons, without knowing the details of how it counts the money, dispenses the item, or keeps track of its inventory.
  6. A microwave oven: A microwave oven has buttons that allow you to set the cooking time and power level, but the internal mechanisms that generate the microwaves and control the cooking process are hidden from the user.
  7. A washing machine: A washing machine has buttons that allow you to select the wash cycle, temperature, and other settings, but the internal mechanisms that fill the tub with water, agitate the clothes, and drain the water are hidden from the user.
  8. A computer’s operating system: An operating system such as Windows or macOS allows you to interact with your computer using a graphical user interface, without knowing the details of how the operating system communicates with the hardware and manages resources.

Leave a Reply