Encapsulation and Data Hiding
Lesson Overview
This lesson explains Encapsulation, a core concept in OOP that restricts direct access to an object's data. It focuses on data hiding to protect internal information from accidental changes or misuse. You'll learn how to "hide" data so it can only be accessed or modified through specific, controlled methods (like the ones from the previous lesson), keeping your code safe and modular.
Lesson Content
Introduction to Encapsulation: The Big Picture
Now that you understand Private Variables (the vault) and Getters/Setters (the security guards), you have already learned the core components of a larger concept called Encapsulation.
What is Encapsulation?
Think of a capsule (medicine). The medicine powder is sealed inside the capsule shell. You swallow the capsule, but you don't touch the powder directly. The shell protects the medicine and delivers it safely.
Encapsulation is similar in programming. It involves bundling data (attributes) and methods (behaviors) together into a single unit (a class) and restricting direct access to some of that data.
It is not just about "hiding" data; it's about control. You are creating a safe, self-contained unit where the internal machinery (private variables) is protected, and the only way to interact with it is through a clean, safe interface (public methods)
You have already built an encapsulated system in the Bank Account example!
class BankAccount:
#BUNDLING (data + methods together)
def __init__(self, name, balance):
self.name = name # Public attribute
self.__balance = balance # Private attribute __balance is PRIVATE (hidden with __)
# CONTROLLED INTERFACE (public methods)
def get_balance(self): # Public method
return self.__balance
def set_balance(self, amount): # Public method with validation
if amount < 0:
print("Error: Cannot be negative")
else:
self.__balance = amount
# Using the encapsulated class:
account = BankAccount("Ajay", 1000)
# Users CAN access through public interface
print(account.get_balance())
account.set_balance(5000)
# Users CANNOT access private data directly
print(account.__balance) # Error
account.__balance = -500 # Error
- Bundling: You combined
name,balance, and the logic (deposit) inside oneclass BankAccount. - Restriction: You used
__balance(Private Variable) to hide the raw data. - Controlled Interface: You used
get_balance()andset_balance()to control access.
Encapsulation in the Real World:
- Social Media (Facebook/Instagram)
- Private: __email, __password, __phone
- Public:
get_profile(),update_profile() - Rule: "View my profile, but only I can edit it.
- E-commerce (Amazon/Flipkart):
- Private: __credit_card_number, __billing_address
- Public:
get_order_history(),place_order() - Rule: "Browse and order, but can't see others' data"
In every case: Bundling + Restriction + Control = Encapsulation
Why is this important?
- Security: Prevents unauthorized access to sensitive data (like the balance).
- Safety: Prevents bugs. Other programmers can't accidentally change an internal variable that ruins your object's logic.
- Flexibility: You can change the internal code (how
__codeis stored) without breaking the code of everyone else who uses your class methods.
Summary:
When you put these pieces together—Classes + Private Variables + Getters/Setters—you are practicing Encapsulation. You are telling the world: "Here is my object. You can use it, but only according to my rules