encapsulation

It means binding data (properties) and the code (methods) that work on that data together in one class.

But more importantly, it’s used to protect your data, so that no one can randomly access or change it from outside.


Let's Understand Through a Simple Scenario

Suppose you're building a class called BankAccount. It has a property called balance.

Now think for a second:

  • Would you want anyone to directly change the balance?

  • Like this:

account.balance = -99999

Of course not.

That’s risky. Because now any user, any class, or even by mistake, the balance can become negative, which makes no sense in real life.

Without Encapsulation – Dangerous Code

class BankAccount:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance

account = BankAccount("Maham", 1000)
print(account.balance)     # 1000

# Anyone can change it!
account.balance = -5000
print(account.balance)     # -5000 (invalid)

This is the problem. Anyone can access and modify your important property directly.

Who is “anyone” in Encapsulation?

When we say “anyone can change the property”, we mean:

1. Other Developers

  • Suppose you're working in a team.

  • Another developer imports your class and uses it in their code.

  • If the property is public, they can directly access and modify it however they like.

from your_code import BankAccount

acc = BankAccount("Maham", 1000)
acc.balance = -9999   # they can change it if it’s public

2. Other Classes

  • A totally different class could access your class's data.

  • If there's no restriction, other classes can misuse or modify the data without following the rules.

3. Even You (by Mistake!)

  • Sometimes, you yourself might accidentally change a property while debugging or testing.
account.balance = "free money"  # by mistake or joke, now it breaks your logic

4. External Users (in large projects)

  • If your class is used in APIs, libraries, or frameworks, external users (you don’t even know them) might use your class incorrectly.

  • Encapsulation helps force them to use it correctly through controlled methods.

Simple Line:

“Anyone” means any code, function, developer, or class that can access your class and doesn’t follow the logic you designed.

So,

Encapsulation stops that from happening — you decide how and when data can be accessed or changed.

With Encapsulation – Safe and Controlled

class BankAccount:
    def __init__(self, name, balance):
        self.name = name
        self.__balance = balance  # made private by using __

    def get_balance(self):
        return self.__balance

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount

Using the Class

account = BankAccount("Maham", 1000)

print(account.get_balance())  # 1000

account.deposit(500)
print(account.get_balance())  # 1500

account.withdraw(300)
print(account.get_balance())  # 1200

# Try to change it directly
account.__balance = -9999     # won't affect the real value
print(account.get_balance())  # still 1200

So What’s Happening?

  • __balance is a private variable

  • It can't be accessed or changed directly

  • You use get_balance(), deposit(), withdraw() methods instead

  • Those methods include logic to protect your data (like no negative deposits)


Real-World Example

Think of an ATM Machine:

  • You can’t directly open the machine and change your bank balance.

  • You must go through the proper interface (insert card, enter pin, click withdraw).

  • That interface checks rules → Do you have enough balance? Is the PIN correct?

Just like that, encapsulation hides the sensitive data and provides a safe way to interact with it.


Key Points

  • Encapsulation = Data hiding + Controlled access

  • Use __ to make properties private

  • Use get and set style methods to access/modify data

  • Prevents invalid data and keeps things safe

Updated on