← Back to Core Java Course | Chapter 7: OOP Core | Lesson 5 of 11

Java के Access Modifiers

Access modifiers decide करते हैं कि किसे एक class के किसी हिस्से को देखने या उपयोग करने की अनुमति है, doors पर locks और keys जैसा। private सिर्फ family के लिए है, और public सबके लिए है।
Syntax
java
public dataType field;     // everywhere
protected dataType field;  // package + subclasses
dataType field;            // package only
private dataType field;    // same class only

What are Access Modifiers?

Access modifiers control करते हैं कि आपके codebase के कौन से हिस्से एक दी class, field, या method देखने और उपयोग करने की अनुमति रखते हैं, encapsulation की basis बनाते हुए — वे वह mechanism हैं जो असल में OOP के 'internal details छुपाओ' principle को enforce करते हैं।

उदाहरण: What are Access Modifiers?

java
// Define the class Account
class Account {
	private double balance = 100;
	public double getBalance() {
		// Send balance back to the caller
		return balance;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Account object and store it in acc
		Account acc = new Account();
		// Print a line to the console
		System.out.println(acc.getBalance());
	}
}

The Private Modifier

private सबसे restrictive level है: एक private member सिर्फ उस class के अंदर visible है जहां इसे declared किया गया, subclasses को भी नहीं। यह internal implementation details के लिए default choice है जिन्हें आप कभी दूसरे code से सीधे छूते नहीं चाहते।

उदाहरण: The Private Modifier

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 to Account
		System.out.println("Balance is private");
	}
}

Default Access

पूरी तरह एक modifier छोड़ना एक member को default (package-private) access देता है, मतलब उसी package में कोई भी class इसे उपयोग कर सकती है, लेकिन दूसरे packages में classes नहीं कर सकतीं — package के बाहर subclasses भी locked out हैं जब तक वे protected या public भी उपयोग न करें।

उदाहरण: Default Access

java
class Account {
	double balance = 100; // no modifier: package-private
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		System.out.println(acc.balance); // visible: same package
	}
}

The Protected Modifier

protected declaring class के अपने package plus कहीं भी किसी भी subclass के लिए visibility खोलता है, एक अलग package में भी — यह उन members के लिए choice का modifier है जिन्हें एक subclass legitimately inherit और उपयोग करने की ज़रूरत है, unrelated code को expose किए बिना।

उदाहरण: The Protected Modifier

java
class Vehicle {
	protected String type = "Generic";
}
class Car extends Vehicle {
	void printType() {
		System.out.println(type); // subclass can access protected member
	}
}
public class Main {
	public static void main(String[] args) {
		new Car().printType();
	}
}

Visibility Control

सबसे tight access level चुनना जो अब भी आपका code काम करने दे एक core defensive-programming habit है: यह आपकी class की internal state को accidental external modification से safe रखता है जबकि अब भी बिल्कुल वे operations expose करता है जो callers को असल में चाहिए।

उदाहरण: Visibility Control

java
class Account {
	private double balance = 100;
	public double getBalance() {
		return balance; // tightest access + controlled exposure
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Account().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. एक field को private बनाना और इसे किसी दूसरी class से access करना, जो एक compile error है।
  2. यह भूल जाना कि कोई modifier न होने का मतलब है package-private access।
  3. हर चीज़ को public बनाना, जो encapsulation तोड़ता है।

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.