What is encapsulation?
Encapsulation is used to hide its members from outside of the class according to the requirement. It encapsulates the complex objects by making those complicated objects as private. In fact encapsulation bringing down the complexity. Actually encapsulation implements abstraction.
In encapsulation:
-
Data Hiding: The internal data and implementation details of a class are hidden from the outside world. This means that the data can only be accessed and modified through controlled methods provided by the class, rather than directly modifying the data from external code.
-
Access Modifiers: Access modifiers, such as public, private, protected, and internal in languages like C#, control the visibility and accessibility of class members (attributes and methods). This allows you to specify which parts of a class are accessible from outside code and which are not.
-
Information Hiding: Encapsulation helps prevent unauthorized access and unintended modifications to the internal state of an object. This leads to better data integrity and security.
-
Abstraction: Encapsulation works hand-in-hand with abstraction. Abstraction defines the external interface of a class while encapsulation ensures that the internal implementation details are hidden from users of the class.
Here's an exmaple in which we are calculating the employee salary, we have exposed one method of the class CalculateSalary() that is abstraction but how the employee is validated and its salary is calculated, and what formulas are implemented to calculate the salary, are all kept hidden. This practice is encapsulation. In other words encapsulate the complex things which are not required by the user into one capsule so that one cannot access it directly.
Let's take another example in C# to understand encapsulation:
public class BankAccount
{
private decimal balance;
public decimal GetBalance()
{
return balance;
}
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
Console.WriteLine("Deposit successful.");
}
}
public void Withdraw(decimal amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
Console.WriteLine("Withdrawal successful.");
}
else
{
Console.WriteLine("Insufficient funds.");
}
}
}
class Program
{
static void Main(string[] args)
{
BankAccount account = new BankAccount();
account.Deposit(1000);
Console.WriteLine("Balance: " + account.GetBalance()); // Output: Balance: 1000
account.Withdraw(500);
Console.WriteLine("Balance: " + account.GetBalance()); // Output: Balance: 500
account.Withdraw(1000);
Console.WriteLine("Balance: " + account.GetBalance()); // Output: Insufficient funds.
}
}
In this example, we have a 'BankAccount' class that encapsulates the concept of a bank account. The 'balance' field is marked as private, meaning it can only be accessed within the class itself. This ensures that the balance cannot be directly modified from external code.
The class provides public methods 'GetBalance()', 'Deposit()', and 'Withdraw()' to interact with the account. The 'GetBalance()' method allows external code to retrieve the account balance. The 'Deposit()' method accepts a positive amount and adds it to the balance. The'Withdraw()' method accepts a positive amount and subtracts it from the balance, but only if there are sufficient funds.
By encapsulating the balance field and providing methods to interact with it, we enforce proper usage of the bank account. External code cannot directly modify the balance field, preventing unauthorized changes. Access to the balance is controlled through the public methods, which perform necessary validations and ensure the integrity of the account's state.
Encapsulation helps in achieving data protection, as well as providing a clear and controlled interface to interact with an object. It promotes better code organization, reusability, and reduces the likelihood of errors and bugs by encapsulating related data and behavior within a single unit.