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:

Join the conversation! Your thoughts help the community grow.