Encapsulation: Encapsulation is one of the fundamental concepts in OOP (Object-Oriented Programming). It involves setting limits on what aspects of data, classes, and methods can be viewed and modified by users. There are three access modifiers: Public, Private, and Protected.
Public:
- Everyone can view and modify it.
- It is the least secure class type.
Protected:
- More secure than the Public modifier.
- Can be viewed or accessed within the same class.
- Can also be viewed or accessed by upper classes, classes derived from it, and classes within the same package.
Private:
- The most secure access modifier.
- Private elements can only be viewed or accessed by the class they are in.
- Both classes and the data they hold can be private.
Benefits of Encapsulation:
- Security: Protects sensitive data from unauthorized access.
- Flexibility: Allows the internal representation of an object to change without affecting the overall system.
- Modularity: Encapsulated code is easier to maintain and update.
Professional Example: Consider a scenario in a banking application where customer account details are encapsulated as private information. The account balance, being a private property, can only be accessed and modified through specific methods defined in the class, ensuring secure and controlled interaction with sensitive financial data.
class BankAccount:
def __init__(self, account_holder, balance):
# Private attributes
self._account_holder = account_holder
self._balance = balance
# Getter method for account holder
def get_account_holder(self):
return self._account_holder
# Setter method for balance with validation
def set_balance(self, new_balance):
if new_balance >= 0:
self._balance = new_balance
else:
print("Invalid balance. Balance cannot be negative.")
# Getter method for balance
def get_balance(self):
return self._balance
# Creating an instance of BankAccount
account1 = BankAccount("Tahsin Soyak", 1000)
# Accessing account holder (get method)
print("Account Holder:", account1.get_account_holder())
# Accessing balance (get method)
print("Initial Balance:", account1.get_balance())
# Trying to set a negative balance (set method)
account1.set_balance(-500)
# Setting a valid balance (set method)
account1.set_balance(1500)
# Accessing updated balance (get method)
print("Updated Balance:", account1.get_balance())
Account Holder: Tahsin Soyak
Initial Balance: 1000
Invalid balance. Balance cannot be negative.
Updated Balance: 1500
- The BankAccount class has private attributes _account_holder and _balance.
- Getter methods (get_account_holder and get_balance) allow access to these private attributes.
- Setter method (set_balance) allows modifying the balance with validation to ensure it's not negative.
- Outside the class, we access and modify the attributes through the getter and setter methods, ensuring encapsulation.
This example showcases encapsulation by restricting direct access to private attributes and providing controlled access through methods.
If you need further information or have specific questions, feel free to ask! Tahsin Soyak tahsinsoyakk@gmail.com
Top comments (0)