Encapsulation and abstraction are two pillars of object-oriented programming (OOP) that play a vital role in writing secure, maintainable, and scalable code. These concepts help developers manage complexity by controlling how data and functionality are exposed to the outside world.
In this post, we’ll explore encapsulation and abstraction in C#, understand their differences, and learn how to use them effectively in your code.
Encapsulation is the practice of bundling data (fields) and methods (functions) that operate on the data into a single unit, typically a class. It also involves restricting direct access to some components, providing controlled access via methods or properties.
private
, protected
, internal
, public
) to control visibility.public class BankAccount
{
private decimal balance; // Private field
public BankAccount(decimal initialBalance)
{
if (initialBalance < 0)
{
throw new ArgumentException("Initial balance cannot be negative.");
}
balance = initialBalance;
}
// Property to get and set the balance
public decimal Balance
{
get { return balance; }
private set // Private setter to prevent external modification
{
if (value < 0)
{
throw new InvalidOperationException("Balance cannot be negative.");
}
balance = value;
}
}
// Method to deposit money
public void Deposit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Deposit amount must be positive.");
}
Balance += amount;
}
// Method to withdraw money
public void Withdraw(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Withdrawal amount must be positive.");
}
if (amount > balance)
{
throw new InvalidOperationException("Insufficient balance.");
}
Balance -= amount;
}
}
// Usage
var account = new BankAccount(100);
account.Deposit(50);
Console.WriteLine(account.Balance); // Output: 150
account.Withdraw(30);
Console.WriteLine(account.Balance); // Output: 120
Here, the balance
field is private, ensuring that it can only be modified through controlled methods like Deposit
and Withdraw
.
Abstraction focuses on hiding implementation details and exposing only the essential features of a class or object. It helps reduce complexity by allowing developers to work with higher-level concepts without needing to understand the intricate details.
Feature | Encapsulation | Abstraction |
---|---|---|
Definition | Hides the internal state of an object | Hides the implementation details |
Focus | Secures data through access control | Simplifies complexity by focusing on behavior |
Implementation | Achieved with access modifiers | Achieved with abstract classes or interfaces |
Purpose | Protect and manage data integrity | Expose essential functionality only |
Encapsulation and abstraction are powerful tools that help manage complexity and protect your code. By applying these principles effectively, you can write robust, secure, and maintainable applications. Mastering these concepts is essential for any aspiring or experienced developer!
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to take on…
Inheritance is a cornerstone of object-oriented programming (OOP) and one of its most powerful features.…
In the world of C# and object-oriented programming (OOP), classes and objects form the backbone…
In modern C# programming, working with data collections is a common task. Understanding how to…
Exception handling is a critical part of writing robust and maintainable C# applications. It allows…
One of the common questions among Docker users is whether Docker containers consume disk space.…