B3.1.5 Explain and apply the concepts of encapsulation and information hiding in OOP.
• The principles of encapsulation and information hiding
• Apply access modifiers such as private and public
• Controlling access to class members
• The importance of limiting access to maintain the integrity and security of an object's state
The big idea
In object-oriented programming, encapsulation and information hiding are about controlling how data inside an object can be accessed and changed.
- Encapsulation means bundling the object’s data (attributes) and methods (behavior) together.
- Information hiding means restricting direct access to some parts of the object, so outside code must use safe, controlled methods to interact with it.
These principles protect an object’s internal state from accidental damage and keep its behavior predictable.
Encapsulation in practice
When you create a class, you can:
- Store data in attributes.
- Create methods to read or change those attributes.
Encapsulation ensures:
- The data is part of the object.
- The only way to change it is through methods you’ve designed.
Information hiding
Information hiding takes encapsulation further by restricting direct access to an object’s attributes. Instead of allowing anyone to change data directly, you make attributes private and provide public methods to safely access or update them.
Benefits:
- Prevents invalid or dangerous changes.
- Allows you to change the internal design without breaking outside code.
- Makes bugs easier to track down.
Access modifiers
Access modifiers control where a class member (attribute or method) can be accessed from:
| Modifier | Meaning | Example usage |
|---|---|---|
public | Can be accessed from anywhere | Methods intended for use outside the class |
private | Accessible only within the same class | Attributes that should not be changed directly |
protected | Accessible within the same class and subclasses | Common in inheritance situations |
In Python, access modifiers aren’t enforced by the language, but naming conventions (
_protected,__private) indicate intent.
Example: BankAccount class
Java:
public class BankAccount {
private double balance; // private: can't change from outside
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public double getBalance() { // public getter
return balance;
}
public void deposit(double amount) { // public method to safely change balance
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) { // public method with rules
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}
Python (using conventions):
class BankAccount:
def __init__(self, initial_balance):
self.__balance = initial_balance # name-mangled "private"
def get_balance(self):
return self.__balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def withdraw(self, amount):
if amount > 0 and amount <= self.__balance:
self.__balance -= amount
How encapsulation and information hiding are applied here:
- The
balanceattribute is private. - Access to
balanceis controlled through public methods (getBalance,deposit,withdraw). - Business rules (e.g., no negative withdrawals) are enforced inside the class.
Why it matters
- Integrity: Keeps the object’s state valid at all times.
- Security: Prevents unauthorized changes.
- Flexibility: You can change how data is stored internally without changing the external interface.
- Maintainability: Other parts of the program don’t need to know how the object works inside.