1Protecting Data with OOP Principles
Hi friends, in this article we will learn encapsulation and abstraction in C#. Both are important concepts in object-oriented programming (OOP), a way of designing software using classes and objects. Encapsulation protects data by controlling access to it. Abstraction hides internal complexity and exposes only what users need. We use these principles in banking, payment applications, APIs and enterprise systems.
2Think About Your ATM
When you withdraw money from an ATM, you enter your PIN and amount. You do not see how the bank verifies your PIN, checks the balance or communicates with its database.
The ATM exposes simple operations like withdraw and balance enquiry. That is abstraction. Your account balance is protected and can only be changed through controlled operations. That is encapsulation.
3Understand Both Concepts
Encapsulation
Encapsulation bundles data and methods inside a class. Access modifiers such as private and public control who can access members.
Private and Public
A private member is accessible only inside its class. A public member can be accessed from outside the class.
Abstraction
Abstraction shows essential operations and hides internal implementation. C# supports abstraction using abstract classes and interfaces.
Abstract Class
An abstract class cannot be directly instantiated. It can define abstract methods that child classes must implement.
| Encapsulation | Abstraction |
|---|---|
| Protects data | Hides complexity |
| Uses access modifiers | Uses interfaces and abstract classes |
| Controls access | Defines what an object can do |
4Build a Secure Bank Account
Here, balance is private. The abstract BankAccount defines a common withdrawal operation. SavingsAccount implements the actual withdrawal behaviour.
using System;
abstract class BankAccount
{
private decimal balance = 1000;
public decimal GetBalance()
{
return balance;
}
public abstract void Withdraw(decimal amount);
protected void Deduct(decimal amount)
{
balance -= amount;
}
}
class SavingsAccount : BankAccount
{
public override void Withdraw(decimal amount)
{
if (amount > 0 && amount <= GetBalance())
{
Deduct(amount);
Console.WriteLine("Withdrawal successful");
}
else
{
Console.WriteLine("Invalid amount or balance");
}
}
}
class Program
{
static void Main()
{
BankAccount account = new SavingsAccount();
account.Withdraw(300);
Console.WriteLine(account.GetBalance());
}
}
The private balance cannot be changed directly. The abstract method defines the operation, while the child class provides its implementation.
5See the Program Result
Withdrawal successful 700
The account starts with 1000. After withdrawing 300, the remaining balance is 700.
6Follow the Account Operations
Watch how the private balance stays protected while the abstract withdrawal operation is implemented by the savings account.
7Key Points to Take Away
Encapsulation protects data from direct access. Abstraction exposes only required operations and hides internal details. Together, they help create secure, maintainable applications.
- Keep sensitive fields private.
- Expose controlled public methods.
- Use abstract classes or interfaces to define behaviour.
8Questions Interviewers May Ask
What is encapsulation in C#?
Answer: Encapsulation combines data and methods in a class and controls access using modifiers such as private and public.
What is abstraction?
Answer: Abstraction hides internal implementation and exposes only essential operations. Interfaces and abstract classes support it.
What is the difference between private and protected?
Answer: Private members are accessible only within their declaring class. Protected members are also accessible in derived classes.
Can we create an object of an abstract class?
Answer: No. An abstract class cannot be instantiated directly. A derived concrete class can inherit and implement it.
How do interfaces support abstraction?
Answer: An interface defines a contract. A class implements that contract and provides the required behaviour without exposing its internal implementation.