← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 6 of 8

Java में Encapsulation

Encapsulation एक object का data private रखना है और दूसरों को इसे सिर्फ safe methods के through उपयोग करने देना है, अपने insides छुपाने वाली एक vending machine जैसा। यह data को accidental changes से protect करता है।
Syntax
java
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?

java
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());
	}
}

Private Data Fields

Fields को private मार्क करना उन्हें class के बाहर के code से पूरी तरह छुपाता है, जो वह mechanism है जिस पर encapsulation असल में निर्भर करता है — private fields के बिना, external code को सीधे अंदर पहुंचकर object की state corrupt करने से कुछ भी नहीं रोकता।

उदाहरण: Private Data Fields

java
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");
	}
}

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

java
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());
	}
}

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

java
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());
	}
}

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

java
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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Fields को public बनाना और getters और setters skip करना।
  2. एक getter से सीधे एक mutable field return करना, इसलिए बाहरी code इसे बदल सकता है।
  3. हर field के लिए एक setter जोड़ना भले ही यह read-only होनी चाहिए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.