Introduction
OOP (Object-Oriented Programming) in C# is a programming approach where you structure your code using objects and classes instead of just functions and logic. It helps make code more organized, reusable, and easier to maintain.
C# is a popular programming language that uses OOP concepts to build applications. With OOP, Instead of writing everything in one place, you break your program into smaller, reusable pieces and you can create reusable code
In this article we will learn how to use oop concept in c# with real world example.
What is OOP
let we take real world entity,
Think about a Car:
It has properties (color, speed, brand)
It has behaviors (start, stop, accelerate)
In OOP:
A class = blueprint of a car
An object = actual car made from that blueprint
The main 4 Pillars of OOP
1. Encapsulation (Data Hiding) : Encapsulation is the concept of hiding internal data and exposing only what is necessary.
A real-world example of encapsulation is a banking system, where a user can perform operations like money transfer or balance inquiry. While the balance is updated and the transaction is completed, but the internal processing logic (such as validation, security checks, and database operations) is hidden from the user.
Example:
using System;
class BankAccount
{
private double balance;
public void Deposit(double amount) {
balance += amount;
}
public double GetBalance(){
return balance;
}
}
class Program
{
static void Main(string[] args){
BankAccount account = new BankAccount();
account.Deposit(1000);
account.Deposit(500);
double currentBalance = account.GetBalance();
Console.WriteLine("Current Balance: " + currentBalance);
}
}
2. Inheritance (Code Reuse) : Inheritance is a concept that we can inherit parent properties and method into child class.
A real-world example is a mobile system, where a smartphone (derived class) inherits basic functionalities such as calling and messaging from a basic phone (base class), and also extends it by adding advanced features like internet access and applications.
Example :
using System;
// Base class (parent)
class BasicPhone
{
public void Call()
{
Console.WriteLine("Making a call...");
}
public void SendMessage()
{
Console.WriteLine("Sending a message...");
}
}
// Derived class (child)
class Smartphone : BasicPhone
{
public void BrowseInternet()
{
Console.WriteLine("Browsing the internet...");
}
public void UseApps()
{
Console.WriteLine("Using apps...");
}
}
class Program
{
static void Main()
{
// Create a smartphone object
Smartphone myPhone = new Smartphone();
// Access inherited methods
myPhone.Call();
myPhone.SendMessage();
// Access new methods in derived class
myPhone.BrowseInternet();
myPhone.UseApps();
}
}
3.Polymorphism (Many Forms) : Polymorphism is a concept that one method is behaves in different ways.
In real example, A method like SendNotification() can work differently for different objects—for example, it can send an email, SMS, or mobile notification. This is polymorphism: one method, many behaviors.
Example:
using System;
// Base class
class Notification
{
public virtual void SendNotification()
{
Console.WriteLine("Sending a generic notification...");
}
}
// Derived class for Email
class EmailNotification : Notification
{
public override void SendNotification()
{
Console.WriteLine("Sending Email Notification");
}
}
// Derived class for SMS
class SMSNotification : Notification
{
public override void SendNotification()
{
Console.WriteLine("Sending SMS Notification");
}
}
// Derived class for Mobile Push
class MobileNotification : Notification
{
public override void SendNotification()
{
Console.WriteLine("Sending Mobile Push Notification");
}
}
class Program
{
static void Main()
{
// Using polymorphism
Notification notification;
notification = new EmailNotification();
notification.SendNotification();
notification = new SMSNotification();
notification.SendNotification();
notification = new MobileNotification();
notification.SendNotification();
}
}
4.Abstraction (Hide Complexity) : Abstraction is a concept in where only essential details are shown to the user, and the internal implementation is hidden.
A real-world example of abstraction is an ATM machine, where a user can withdraw cash, check balance, or deposit money. While the transaction is completed, the internal processing (such as account verification, database updates, and security checks) is hidden from the user.
Example:
using System;
abstract class ATM
{
public abstract void Withdraw(double amount);
public abstract void CheckBalance();
}
class MyATM : ATM
{
private double balance = 3000; // hidden balance
public override void Withdraw(double amount)
{
if (amount <= balance)
{
balance -= amount;
Console.WriteLine($"Withdrawn: {amount}, Remaining balance: {balance}");
}
else
{
Console.WriteLine("Not enough balance!");
}
}
public override void CheckBalance()
{
Console.WriteLine($"Current balance: {balance}");
}
}
class Program
{
static void Main()
{
ATM atm = new MyATM();
atm.CheckBalance(); // Shows balance
atm.Withdraw(1000); // Withdraws money
atm.CheckBalance(); // Shows updated balance
}
}
Why OOP is Useful
| OOP Concept | Real-Life Example |
|---|
| Class | Blueprint of a house |
| Object | Actual house |
| Encapsulation | Locked doors |
| Inheritance | Child inherits traits |
| Polymorphism | Same action, different results |
| Abstraction | Car driving (you don’t see engine details) |
Conslusion
Object-Oriented Programming helps make code clean, reusable, and easy to understand. By learning its core concepts, you can build better and more structured applications in C#.
I hope this helps you !