DEV Community

Sandeep Borhade
Sandeep Borhade

Posted on • Edited on

1

Encapsulation vs Abstraction

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (1)

Collapse
 
agunechemba profile image
Agunechemba Ekene

hmmm?

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay