OOP Concepts
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is OOP?
Object-Oriented Programming (OOP) is a way of writing programs by organizing code around objects instead of just functions and logic. An object bundles together data (fields) and actions (methods) that work on that data — just like a real-world object such as a car has properties (color, speed) and actions (start, brake).
Java is a fully object-oriented language — almost everything in Java lives inside a class. OOP makes large programs easier to understand, reuse and maintain, which is why it is the foundation of Android, enterprise and banking software.
Think of a class as a blueprint (like a house design) and an object as the actual house built from it. One blueprint → many houses. One class → many objects.
The Four Pillars of OOP
Every OOP concept is built on four core principles, often called the four pillars:
| Pillar | Meaning | Simple Example |
|---|---|---|
| Encapsulation | Bundle data + methods together and hide internal details using private fields with getters/setters. | ATM hides your balance; you use buttons, not the internal wiring. |
| Inheritance | One class reuses the fields and methods of another using extends. | A Car and Bike both inherit from Vehicle. |
| Polymorphism | One method name behaves differently for different objects (overloading & overriding). | draw() works for Circle, Square, Triangle. |
| Abstraction | Show only the essential features, hide the complex implementation using abstract classes/interfaces. | You drive a car without knowing how the engine works. |
A Complete Example
This small program shows a class, an object, encapsulation (private field + methods) and inheritance together:
class Animal { private String name; // encapsulation: private field Animal(String name) { this.name = name; } void eat() { System.out.println(name + " is eating"); } } class Dog extends Animal { // inheritance Dog(String n) { super(n); } void bark() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Dog d = new Dog("Tommy"); // object creation d.eat(); // inherited from Animal d.bark(); // Dog's own method } }
Tommy is eating Woof!
Why Use OOP?
- Reusability — write code once (a class) and reuse it via objects and inheritance.
- Maintainability — changes stay contained; fixing one class doesn't break others.
- Security — encapsulation hides sensitive data behind controlled methods.
- Real-world modelling — code mirrors real objects, so it's easier to reason about.
Students mix up class and object. Remember: a class is written once (the blueprint); objects are created many times from it at runtime using new.
Summary
- OOP organizes code around objects = data (fields) + actions (methods).
- Four pillars: Encapsulation, Inheritance, Polymorphism, Abstraction.
- A class is a blueprint; an object is a real instance created with
new. - OOP gives reusability, maintainability, security and real-world modelling.
OOP क्या है?
Object-Oriented Programming (OOP) programs लिखने का एक तरीका है जिसमें code को सिर्फ़ functions और logic के बजाय objects के इर्द-गिर्द organize किया जाता है। एक object में data (fields) और उस data पर काम करने वाले actions (methods) दोनों एक साथ होते हैं — जैसे असल दुनिया में एक car के properties (रंग, speed) और actions (start, brake) होते हैं।
Java पूरी तरह object-oriented language है — इसमें लगभग सब कुछ किसी class के अंदर होता है। OOP बड़े programs को समझने, reuse करने और maintain करने में आसान बनाता है, इसीलिए यह Android, enterprise और banking software की नींव है।
class को एक blueprint (जैसे घर का नक्शा) समझें और object को उस नक्शे से बना असली घर। एक blueprint → कई घर। एक class → कई objects।
OOP के चार स्तंभ
हर OOP concept चार मूल सिद्धांतों पर बना है, जिन्हें चार स्तंभ कहते हैं:
| स्तंभ | मतलब | आसान उदाहरण |
|---|---|---|
| Encapsulation | private fields और getters/setters से data + methods को एक साथ रखना और अंदरूनी details छुपाना। | ATM आपका balance छुपाता है; आप buttons use करते हैं, अंदरूनी wiring नहीं। |
| Inheritance | extends से एक class दूसरी के fields और methods reuse करती है। | Car और Bike दोनों Vehicle से inherit करते हैं। |
| Polymorphism | एक ही method नाम अलग-अलग objects के लिए अलग व्यवहार करता है। | draw() Circle, Square, Triangle सबके लिए काम करता है। |
| Abstraction | abstract classes/interfaces से सिर्फ़ ज़रूरी चीज़ें दिखाना, जटिलता छुपाना। | आप car चलाते हैं बिना engine की अंदरूनी बातें जाने। |
एक पूरा उदाहरण
यह छोटा program एक class, object, encapsulation (private field + methods) और inheritance एक साथ दिखाता है:
class Animal { private String name; // encapsulation Animal(String name) { this.name = name; } void eat() { System.out.println(name + " is eating"); } } class Dog extends Animal { // inheritance Dog(String n) { super(n); } void bark() { System.out.println("Woof!"); } }
Tommy is eating Woof!
OOP क्यों use करें?
- Reusability — code एक बार लिखें (class) और objects व inheritance से बार-बार use करें।
- Maintainability — बदलाव सीमित रहते हैं; एक class ठीक करने से दूसरी नहीं टूटती।
- Security — encapsulation संवेदनशील data को controlled methods के पीछे छुपाता है।
- असल दुनिया से मेल — code असली objects जैसा होता है, समझना आसान।
छात्र class और object में confuse होते हैं। याद रखें: class एक बार लिखी जाती है (blueprint); objects new से runtime पर कई बार बनते हैं।
सारांश
- OOP code को objects = data (fields) + actions (methods) के इर्द-गिर्द organize करता है।
- चार स्तंभ: Encapsulation, Inheritance, Polymorphism, Abstraction।
- class blueprint है; object
newसे बना असली instance। - OOP देता है reusability, maintainability, security और real-world modelling।