Java में Encapsulation
In this page:
class ClassName {
private dataType field;
public dataType getField() { return field; }
public void setField(dataType value) { this.field = value; }
}
What is Encapsulation?
Encapsulation एक object का data (fields) और इस पर operate करने वाला code (methods) को एक unit में bundle करता है, फिर उस data तक direct बाहरी access restrict करता है — object की internal state के आस-पास एक protective shell जैसा काम करते हुए।
उदाहरण: What is Encapsulation?
class Account {
private double balance; // data and methods bundled together
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(50);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
Private Data Fields
Fields को private मार्क करना उन्हें class के बाहर के code से पूरी तरह छुपाता है, जो वह mechanism है जिस पर encapsulation असल में निर्भर करता है — private fields के बिना, external code को सीधे अंदर पहुंचकर object की state corrupt करने से कुछ भी नहीं रोकता।
उदाहरण: Private Data Fields
class Account {
private double balance = 100;
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
// acc.balance would not compile: private field
System.out.println("Field is hidden from outside code");
}
}
Login to try C/C++/Java/PHP code in the editor
Getter and Setter Methods
Getters और setters उन hidden private fields तक controlled, indirect access देते हैं, और चूंकि एक setter असली code है (एक bare assignment नहीं), यह field update करने की अनुमति मिलने से पहले incoming values validate कर सकता है — bad data reject करते हुए।
उदाहरण: Getter and Setter Methods
class Account {
private double balance;
public void setBalance(double balance) {
if (balance >= 0) { // validation
this.balance = balance;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.setBalance(-10);
acc.setBalance(200);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
Read-Only and Write-Only Classes
Setters skip करना एक effectively read-only class produce करता है (state inspect हो सकती है लेकिन construction के बाद कभी नहीं बदली जा सकती), जबकि getters skip करना एक write-only class produce करता है — callers को एक value देखनी है या सिर्फ set करनी है इस पर निर्भर दोनों उपयोगी patterns हैं।
उदाहरण: Read-Only and Write-Only Classes
class ReadOnly {
private final int id = 7;
public int getId() { return id; } // no setter
}
public class Main {
public static void main(String[] args) {
System.out.println(new ReadOnly().getId());
}
}
Login to try C/C++/Java/PHP code in the editor
Benefits of Encapsulation
अच्छी तरह encapsulated classes invalid state से protect करती हैं, isolation में unit test करना कहीं आसान हैं, और आपको बाद में internal implementation details freely refactor करने देती हैं बिना किसी external code को तोड़े, क्योंकि callers हमेशा सिर्फ stable public getter/setter contract पर निर्भर करते हैं।
उदाहरण: Benefits of Encapsulation
class Account {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount; // protects against invalid state
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(-50); // rejected
acc.deposit(100);
System.out.println(acc.getBalance());
}
}
Login to try C/C++/Java/PHP code in the editor
- Fields को public बनाना और getters और setters skip करना।
- एक getter से सीधे एक mutable field return करना, इसलिए बाहरी code इसे बदल सकता है।
- हर field के लिए एक setter जोड़ना भले ही यह read-only होनी चाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: