Encapsulation and abstraction are two important concepts in object-oriented programming.
Encapsulation refers to the practice of “encapsulating” data and methods within a single unit, called a class. This means that the class provides specific methods to access and modify the data while hiding the data itself from outside code. Encapsulation provides a way to protect the data from unintended changes, ensuring that the class behaves as expected.
Abstraction refers to the practice of hiding the complexity of a system by providing a simplified interface. This means that the user of a class only needs to know how to use the methods provided by the class, without having to worry about how the class actually works. Abstraction provides a way to manage complexity, making it easier to use and maintain software systems.
To achieve encapsulation in Python, one can use private variables and methods marked with a double underscore prefix. Accessing such variables and methods directly from outside the class is not possible, but public methods defined within the class can provide access to them.
Interfaces can achieve abstraction by defining a set of methods that any class implementing the interface must implement. This allows users of the interface to interact with any class that implements it, without having to know the specific implementation details of the class.
Here’s an example of encapsulation and abstraction in Python:
class BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount > self.__balance: raise ValueError("Insufficient balance") self.__balance -= amount def get_balance(self): return self.__balance class Bank: def __init__(self): self.__accounts = [] def add_account(self, account): self.__accounts.append(account) def total_balance(self): return sum(account.get_balance() for account in self.__accounts)
In this example, BankAccount
is a class that represents a bank account. The balance
of the account is stored in a private variable __balance
, and can only be accessed or modified through the public methods deposit
, withdraw
, and get_balance
.
Bank
is another class that represents a bank, and contains a list of BankAccount
objects. The add_account
method is used to add an account to the list, and the total_balance
method is used to calculate the total balance of all accounts in the list.
By using encapsulation, the BankAccount
class ensures that the balance
data is protected and can only be accessed or modified through specific methods. By using abstraction, the Bank
class provides a simplified interface for interacting with bank accounts, without having to know the specific implementation details of the BankAccount
class.
Example
class BankAccount: def __init__(self, account_number, balance): self.__account_number = account_number self.__balance = balance def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount else: print("Insufficient funds.") def get_balance(self): return self.__balance account = BankAccount("123456", 1000) account.deposit(500) print(account.get_balance()) # Output: 1500 account.withdraw(2000) # Output: Insufficient funds. print(account.get_balance()) # Output: 1500
In this example, we define a class BankAccount
that has two private attributes __account_number
and __balance
. We make these attributes private using double underscores, which encapsulates them and prevent direct access from outside the class..
The class also has three methods: deposit
, withdraw
, and get_balance
. The deposit
the method allows deposits to be made to the account, the withdraw
method allows withdrawals to be made as long as there are sufficient funds, and the get_balance
method returns the current balance of the account.
These methods simplify the account by abstracting away details such as how the balance is calculated or where the account is stored. As a result, users can interact with the account using a simplified interface provided by these methods This showcases how abstraction can simplify the complexity of a system and offer a higher-level interface for users