Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 22

Encapsulation

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What is Encapsulation?

Encapsulation means wrapping the data (variables) and the code (methods) that work on that data into a single unit — a class — and hiding the internal details from the outside world. In Java it is achieved by making fields private and giving controlled access through public getter and setter methods.

💡 Real-world analogy

A medicine capsule wraps the medicine inside a shell — you can't touch the powder directly. Similarly, encapsulation wraps data inside a class and you access it only through methods.

How to Achieve Encapsulation

  • Declare the class fields as private.
  • Provide public getter methods to read them.
  • Provide public setter methods to change them (with validation).
Java
class Account {
    private double balance;   // hidden data

    public double getBalance() {   // getter
        return balance;
    }
    public void deposit(double amt) {  // controlled setter
        if (amt > 0) balance += amt;   // validation!
    }
}
public class Main {
    public static void main(String[] a) {
        Account ac = new Account();
        ac.deposit(500);
        System.out.println(ac.getBalance());
    }
}
▶ Output
500.0

Benefits of Encapsulation

BenefitWhy it matters
Data hidingOutside code can't directly change balance to an invalid value.
ControlThe setter can validate input (no negative deposit).
FlexibilityYou can change the internal code later without breaking other classes.
Read-only / write-onlyProvide only a getter (read-only) or only a setter (write-only) as needed.
⚠️ Common mistake

Making fields public defeats encapsulation — anyone can then set balance = -9999. Always keep fields private.

Summary

  • Encapsulation = data + methods in one class, with internal details hidden.
  • Achieved by private fields + public getters/setters.
  • Gives data hiding, validation, flexibility and controlled access.

Encapsulation क्या है?

Encapsulation का मतलब है data (variables) और उस data पर काम करने वाले code (methods) को एक ही unit — class — में लपेटना और अंदरूनी details को बाहर से छुपाना। Java में यह fields को private बनाकर और public getter/setter methods से controlled access देकर किया जाता है।

💡 असल दुनिया की मिसाल

दवा का capsule दवा को खोल में लपेटता है — आप powder को सीधे नहीं छू सकते। वैसे ही encapsulation data को class में लपेटता है और आप उसे सिर्फ़ methods से access करते हैं।

Encapsulation कैसे करें

  • class के fields को private रखें।
  • पढ़ने के लिए public getter methods दें।
  • बदलने के लिए public setter methods दें (validation के साथ)।
Java
class Account {
    private double balance;   // छुपा हुआ data
    public double getBalance() { return balance; }
    public void deposit(double amt) {
        if (amt > 0) balance += amt;  // validation
    }
}
▶ Output
500.0

Encapsulation के फ़ायदे

फ़ायदाक्यों ज़रूरी
Data hidingबाहरी code सीधे balance को गलत value नहीं दे सकता।
Controlsetter input validate कर सकता है (negative deposit नहीं)।
Flexibilityबाद में अंदरूनी code बदल सकते हैं बिना दूसरी classes तोड़े।
⚠️ आम गलती

fields को public बनाना encapsulation खत्म कर देता है — कोई भी balance = -9999 कर सकता है। हमेशा fields private रखें।

सारांश

  • Encapsulation = data + methods एक class में, अंदरूनी details छुपी हुई।
  • private fields + public getters/setters से किया जाता है।
  • देता है data hiding, validation, flexibility और controlled access।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.