LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Encapsulation and Abstraction Explained

Abstraction hides internal complexity and exposes only what users need. We use these principles in banking, payment applications, APIs and enterprise systems.

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.

Remember Encapsulation controls access to data. Abstraction hides implementation details.

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
Pro Tip Use private fields and public methods to validate data before changing it.
Common Mistake Making every field public exposes data and allows uncontrolled changes.

4Build a Secure Bank Account

Here, balance is private. The abstract BankAccount defines a common withdrawal operation. SavingsAccount implements the actual withdrawal behaviour.

Program.csC#
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

Output
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.

Program.cs - Visual Studio Code
▤⌕⑂▦
● Program.cs
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
ReadyUTF-8  CRLF  C#

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

Q1Easy

What is encapsulation in C#?

Answer: Encapsulation combines data and methods in a class and controls access using modifiers such as private and public.

Q2Easy

What is abstraction?

Answer: Abstraction hides internal implementation and exposes only essential operations. Interfaces and abstract classes support it.

Q3Medium

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.

Q4Medium

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.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.