Abstraction vs Encapsulation in C#
What is Abstraction?
Definition : Explain abstraction as the process of hiding implementation details and showing only the essential features of an object.
Focuses on what an object does rather than how it does it.
Achieved through abstract classes, interfaces, and polymorphism.
Real-World Analogy : E.g., Using a TV remote—users know the buttons to press but not the internal mechanics.
Practical Example in C# :
// Abstract class
abstract class Animal
{
public abstract void Speak ();
}
// Derived classes
class Dog : Animal
{
public override void Speak () => Console . WriteLine ( "Bark" );
}
class Cat : Animal
{
public override void Speak () => Console . WriteLine ( "Meow" );
}
// Usage
class Program
{
static void Main ()
{
Animal dog = new Dog ();
dog . Speak (); // Output: Bark
Animal cat = new Cat ();
cat . Speak (); // Output: Meow
}
}
Enter fullscreen mode
Exit fullscreen mode
What is Encapsulation?
Definition : Encapsulation is the process of wrapping data (fields) and code (methods) together into a single unit, often restricting access to some components.
Focuses on controlling access using access modifiers (public, private, protected, internal).
Protects the integrity of an object’s state by restricting direct access to its fields.
Real-World Analogy : E.g., Bank account system where only authorized operations are allowed through an interface.
Practical Example in C# :
class BankAccount
{
private decimal balance ;
public decimal GetBalance () => balance ;
public void Deposit ( decimal amount )
{
if ( amount > 0 )
{
balance += amount ;
}
}
public void Withdraw ( decimal amount )
{
if ( amount > 0 && amount <= balance )
{
balance -= amount ;
}
}
}
// Usage
class Program
{
static void Main ()
{
BankAccount account = new BankAccount ();
account . Deposit ( 1000 );
account . Withdraw ( 500 );
Console . WriteLine ( account . GetBalance ()); // Output: 500
}
}
Enter fullscreen mode
Exit fullscreen mode
Key Differences Between Abstraction and Encapsulation
Aspect
Abstraction
Encapsulation
Focus
Hiding implementation details.
Hiding object state and protecting it.
Achieved Using
Abstract classes, interfaces.
Access modifiers (private, public, etc.).
Purpose
Simplifies complex systems.
Ensures controlled access and security.
Example
Animal classes showing behavior.
Bank account managing balance.
How Abstraction and Encapsulation Work Together
Highlight how abstraction defines the behavior and encapsulation protects the data.
Provide a combined example:
csharp
abstract class Employee
{
public abstract string GetRole();
}
class Developer : Employee
{
private string name;
private decimal salary;
public Developer(string name, decimal salary)
{
this.name = name;
this.salary = salary;
}
public override string GetRole() => "Developer";
public decimal GetSalary() => salary;
}
// Usage
class Program
{
static void Main()
{
Employee dev = new Developer("Alice", 60000);
Console.WriteLine(dev.GetRole()); // Output: Developer
// Cannot access salary directly; encapsulated for protection.
}
}
Enter fullscreen mode
Exit fullscreen mode
Top comments (1)
hmmm?