C# > Object-Oriented Programming (OOP) > Encapsulation > Access Modifiers for Members
Encapsulation with Access Modifiers in C#
This code demonstrates encapsulation using access modifiers in C#. Encapsulation is a fundamental principle of object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on that data within a single unit, known as a class. Access modifiers control the visibility and accessibility of class members.
Code Example
This example demonstrates a `BankAccount` class. The `accountNumber` and `balance` fields are declared as `private`. This means they can only be accessed from within the `BankAccount` class itself. Public properties `AccountNumber` and `Balance` provide controlled access to these fields. The `Deposit` and `Withdraw` methods provide controlled ways to modify the balance. Trying to access `balance` directly from outside the class will result in a compile-time error, enforcing encapsulation. This approach protects the internal state of the object and prevents unauthorized modifications.
using System;
public class BankAccount
{
private string accountNumber;
private decimal balance;
public BankAccount(string accountNumber, decimal initialBalance)
{
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public string AccountNumber
{
get { return accountNumber; }
}
public decimal Balance
{
get { return balance; }
}
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
Console.WriteLine($"Deposited {amount:C}. New balance: {balance:C}");
}
else
{
Console.WriteLine("Deposit amount must be positive.");
}
}
public void Withdraw(decimal amount)
{
if (amount > 0 && balance >= amount)
{
balance -= amount;
Console.WriteLine($"Withdrawn {amount:C}. New balance: {balance:C}");
}
else
{
Console.WriteLine("Insufficient funds or invalid withdrawal amount.");
}
}
}
public class Program
{
public static void Main(string[] args)
{
BankAccount myAccount = new BankAccount("1234567890", 1000);
Console.WriteLine($"Account Number: {myAccount.AccountNumber}");
Console.WriteLine($"Initial Balance: {myAccount.Balance:C}");
myAccount.Deposit(500);
myAccount.Withdraw(200);
// Trying to directly access the private balance would result in a compilation error.
// Console.WriteLine(myAccount.balance); // This line would cause an error.
Console.ReadKey();
}
}
Concepts Behind the Snippet
The key concepts demonstrated here are:
Real-Life Use Case
Consider a system managing employee data. Sensitive information like salary and social security number should be encapsulated. You could use private fields for these attributes and provide controlled access through methods or properties. For example, only authorized personnel (e.g., HR) should be able to view or modify the salary. Encapsulation ensures data integrity and security.
Best Practices
Interview Tip
When discussing encapsulation, emphasize that it's not just about hiding data. It's about controlling access and preventing direct manipulation of internal state, which leads to more robust and maintainable code. Explain how access modifiers and properties play a crucial role in achieving encapsulation.
When to Use Them
Use encapsulation whenever you want to protect the internal state of an object and control how it's accessed and modified. This is especially important for complex objects with sensitive data or when you want to enforce specific business rules.
Memory Footprint
Encapsulation itself doesn't directly affect memory footprint. The memory footprint of a class is determined by the data members it contains (fields). Access modifiers don't change the amount of memory allocated to the object. They only control access to that memory.
Alternatives
While access modifiers are the primary way to enforce encapsulation, other techniques can contribute to data protection:
Pros
Cons
FAQ
-
Why use properties instead of directly accessing public fields?
Properties provide a level of indirection that allows you to add logic to the getter and setter. For example, you can perform validation, raise events, or calculate a derived value when the property is accessed or modified. -
What is the difference between `private` and `protected`?
`private` members are only accessible within the class where they are declared. `protected` members are accessible within the class where they are declared and in any derived classes, even if those derived classes are in a different assembly.