📖 Introduction
C# me OOPS (Object-Oriented Programming System) seekhna easy ho sakta hai agar aap theory ko ek relatable kahani se connect karein.
Is post me hum Sharma Ji ka Ghar ki story ke through 10 important OOPS concepts samjhenge — taaki aap lifetime yaad rakho.
🏡 Story: Sharma Ji ka Ghar – 10 OOPS Concepts
1. Encapsulation – Data ko Protect Karna
Sharma Ji ke ghar me ek locker tha jisme gold coins rakhe the. Locker ka key sirf Sharma Ji ke paas tha. Bahar wale sirf Deposit() aur Withdraw() methods se coins le ya rakh sakte the.
public class Ghar
{
private int lockerGoldCoins;
public void Deposit(int coins) => lockerGoldCoins += coins;
public void Withdraw(int coins)
{
if (coins <= lockerGoldCoins) lockerGoldCoins -= coins;
}
}
2. Inheritance – Properties ka Virasat Me Milna
Sharma Ji ka beta Rohan ne apna ghar banaya, jo papa ke ghar ka blueprint use karta tha, lekin ek gaming room add kar liya.
public class RohanKaGhar : Ghar
{
public void GamingRoom() => Console.WriteLine("Welcome to Gaming Room!");
}
3. Polymorphism – Ek Naam, Alag Kaam
Dono ghar me DoorBell() method tha, lekin Sharma Ji ke ghar me “Ting Tong” aur Rohan ke ghar me “Ding Dong with Music” bajta tha.
public class Ghar
{
public virtual void DoorBell() => Console.WriteLine("Ting Tong");
}
public class RohanKaGhar : Ghar
{
public override void DoorBell() => Console.WriteLine("Ding Dong with Music 🎵");
}
4. Abstraction – Sirf Zaroori Cheez Dikhana
Guest ko sirf drawing room aur garden dikhaya jaata tha, wiring aur plumbing ka system nahi.
public abstract class GuestView
{
public abstract void ShowDrawingRoom();
}
public class SharmaJiGuestView : GuestView
{
public override void ShowDrawingRoom() =>
Console.WriteLine("Beautiful Drawing Room with Garden View");
}
5. Constructor & Destructor – Ghar ka Nirman aur Tod‑Phod
Constructor ghar banate waqt kaam karta hai, destructor todte waqt.
public class Ghar
{
public Ghar() => Console.WriteLine("Ghar ban gaya!");
~Ghar() => Console.WriteLine("Ghar tod diya gaya!");
}
6. Static Members – Mohalle ka Common Park
Mohalle ka park sabke liye common tha — ek hi copy, sab share karte hain.
public class Mohalla
{
public static string ParkName = "Green Park";
}
7. Interfaces – Ghar ke Rules
Housing society ke rules: har ghar me gate aur doorbell hoga. Implementation har ghar apne hisaab se karega.
public interface IGharRules
{
void Gate();
void DoorBell();
}
8. Method Overloading – Ek Naam, Multiple Tarike
Invite method ke alag‑alag versions.
public void Invite(string name) { }
public void Invite(string name, string plusOne) { }
9. Sealed Class – Ghar jo Modify Nahi Ho Sakta
Heritage property jisme koi badlaav allowed nahi.
public sealed class HeritageGhar { }
10. Abstract Class vs Interface – Blueprint vs Rules
- Abstract Class: Kuch ready‑made rooms + kuch banane padenge.
- Interface: Sirf rules ka paper, implementation aapko karni hai.
📌 Recap Table
| Concept | Story Example | C# Feature |
|---|---|---|
| Encapsulation | Locker with key | private + public methods |
| Inheritance | Rohan ka ghar blueprint | : (extends) |
| Polymorphism | DoorBell sound change | virtual + override |
| Abstraction | Sirf drawing room dikhana | abstract class |
| Constructor/Destructor | Ghar banate/todte waqt kaam | ctor / ~dtor |
| Static Members | Mohalle ka common park | static keyword |
| Interface | Housing Society rules | interface |
| Method Overloading | Invite method ke alag tarike | Same name, diff parameters |
| Sealed Class | Heritage property | sealed keyword |
| Abstract vs Interface | Blueprint vs Rules | abstract vs interface |
🎯 Conclusion
Is kahani ke through aapne seekha:
- Encapsulation → Data protection
- Inheritance → Code reuse
- Polymorphism → Flexible behavior
- Abstraction → Hide complexity
- Aur extra concepts jo interviews me kaam aate hain