Encapsulation
Written and reviewed by Gagan Bhardwaj · 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.
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
publicgetter methods to read them. - Provide
publicsetter methods to change them (with validation).
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()); } }
500.0
Benefits of Encapsulation
| Benefit | Why it matters |
|---|---|
| Data hiding | Outside code can't directly change balance to an invalid value. |
| Control | The setter can validate input (no negative deposit). |
| Flexibility | You can change the internal code later without breaking other classes. |
| Read-only / write-only | Provide only a getter (read-only) or only a setter (write-only) as needed. |
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
privatefields +publicgetters/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रखें। - पढ़ने के लिए
publicgetter methods दें। - बदलने के लिए
publicsetter methods दें (validation के साथ)।
class Account { private double balance; // छुपा हुआ data public double getBalance() { return balance; } public void deposit(double amt) { if (amt > 0) balance += amt; // validation } }
500.0
Encapsulation के फ़ायदे
| फ़ायदा | क्यों ज़रूरी |
|---|---|
| Data hiding | बाहरी code सीधे balance को गलत value नहीं दे सकता। |
| Control | setter input validate कर सकता है (negative deposit नहीं)। |
| Flexibility | बाद में अंदरूनी code बदल सकते हैं बिना दूसरी classes तोड़े। |
fields को public बनाना encapsulation खत्म कर देता है — कोई भी balance = -9999 कर सकता है। हमेशा fields private रखें।
सारांश
- Encapsulation = data + methods एक class में, अंदरूनी details छुपी हुई।
privatefields +publicgetters/setters से किया जाता है।- देता है data hiding, validation, flexibility और controlled access।