Lesson 6 — Abstraction in C#

Abstraction is the second pillar of Object-Oriented Programming (OOP).
If encapsulation focuses on protecting data, abstraction focuses on hiding complexity.

In simple words:

Abstraction means showing only the necessary details and hiding the unnecessary ones.

This makes software easier to use, understand, and maintain.


🌟 What is Abstraction?

Abstraction hides internal implementation details and exposes only essential features to the user.

✔ Simple Definition:

Abstraction = Showing what an object can do, not how it does it.

This helps the developer focus on what something does rather than how it actually works.


📱 Real-World Analogy of Abstraction

📱 Mobile Phone Example

You press:

  • Call button → phone dials
  • Camera icon → photo is taken
  • Volume button → sound changes

But you never see:

  • The code behind calling
  • The algorithm behind focusing the camera
  • The circuit controlling the volume

You only see the interface, not the complexity inside.

This is abstraction.


🛠 Abstraction in C# — How It Works

Abstraction in C# is implemented using:

✔ 1. Abstract Classes

✔ 2. Interfaces

Both hide implementation details and provide a contract for other classes.


💡 1️⃣ Abstraction Using Abstract Classes

An abstract class:

  • Cannot be instantiated
  • May contain abstract methods (no body)
  • Must be inherited by another class
  • Defines a partial blueprint

🧑‍💻 Example — Abstract Class

public abstract class Animal
{
    public abstract void MakeSound();  // abstract method

    public void Sleep()                // normal method
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Bark!");
    }
}

Using the class:

Animal a = new Dog();
a.MakeSound();   // Bark!
a.Sleep();       // Sleeping...

Here:

  • You call MakeSound() without knowing how Dog implements it
  • This is abstraction

💡 2️⃣ Abstraction Using Interfaces

An interface:

  • Contains only method signatures (no implementation)
  • Must be implemented by classes
  • Provides a 100% abstraction layer

🧑‍💻 Example — Interface

public interface ICamera
{
    void TakePhoto();
}

public class Mobile : ICamera
{
    public void TakePhoto()
    {
        Console.WriteLine("Click! Photo taken.");
    }
}

Using the interface:

ICamera cam = new Mobile();
cam.TakePhoto();

You use the method TakePhoto() without caring how it works internally.


🔍 Abstract Class vs Interface (Simple Table)

FeatureAbstract ClassInterface
MethodsCan have both abstract + non-abstractOnly abstract methods (C# 8+ allows default methods)
FieldsCan have fieldsNo fields allowed
Multiple InheritanceNoYes, a class can implement many interfaces
Use CaseCommon base for related classesContract for unrelated classes

🧠 Why Do We Need Abstraction?

✔ Hides complex code

You don’t expose sensitive logic.

✔ Simplifies usage

Users interact with clean, simple methods.

✔ Improves security

Internal code cannot be misused.

✔ Separates design from implementation

Coders can work independently on different layers.

✔ Allows multiple implementations

Common in frameworks and APIs.


🧱 Real-World Example — Payment System

A payment gateway may support:

  • Credit Card
  • UPI
  • Net Banking
  • Wallet

Abstract representation:

public abstract class Payment
{
    public abstract void Pay(double amount);
}

Different implementations:

public class UpiPayment : Payment
{
    public override void Pay(double amount)
    {
        Console.WriteLine($"Paid {amount} using UPI");
    }
}

public class CreditCardPayment : Payment
{
    public override void Pay(double amount)
    {
        Console.WriteLine($"Paid {amount} using Credit Card");
    }
}

Using abstraction:

Payment p = new UpiPayment();
p.Pay(500);

You only know “Pay()”, not how UPI internally works.


🎯 Encapsulation vs Abstraction (Common Confusion)

EncapsulationAbstraction
Protects dataHides implementation
Achieved with private fields and propertiesAchieved with abstract classes and interfaces
Focus on how data is accessedFocus on what functions are available
Example: private balanceExample: MakePayment() method

Simple explanation:

Encapsulation = “Don’t touch my data.”
Abstraction = “Don’t worry about how I work.”


📝 Mini Exercise

Create:

1️⃣ An abstract class Shape with abstract method Area()
2️⃣ Create two child classes: Circle and Rectangle
3️⃣ Implement the Area() method in both
4️⃣ Print:

Area of circle: ___
Area of rectangle: ___


🔎 FAQs

Q1: Can abstraction exist without encapsulation?

No. Abstraction often depends on encapsulation to hide the internal details.

Q2: Can an abstract class have a constructor?

Yes, abstract classes can have constructors.

Q3: Is interface better than abstract class?

Depends on the use case.
Interfaces = 100% abstraction, multiple inheritance
Abstract classes = shared logic, partial abstraction


🎉 Conclusion

Abstraction is all about hiding complexity and exposing essential features only.
In C#, abstraction is implemented using abstract classes and interfaces.

You now understand:

✔ What abstraction is
✔ Why it is used
✔ Real-world examples
✔ Abstract class and interface use cases
✔ How it simplifies code