Java के Access Modifiers
In this page:
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?
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
The Private Modifier
private सबसे restrictive level है: एक private member सिर्फ उस class के अंदर visible है जहां इसे declared किया गया, subclasses को भी नहीं। यह internal implementation details के लिए default choice है जिन्हें आप कभी दूसरे code से सीधे छूते नहीं चाहते।
उदाहरण: The Private Modifier
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Default Access
पूरी तरह एक modifier छोड़ना एक member को default (package-private) access देता है, मतलब उसी package में कोई भी class इसे उपयोग कर सकती है, लेकिन दूसरे packages में classes नहीं कर सकतीं — package के बाहर subclasses भी locked out हैं जब तक वे protected या public भी उपयोग न करें।
उदाहरण: Default Access
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
}
}
Login to try C/C++/Java/PHP code in the editor
The Protected Modifier
protected declaring class के अपने package plus कहीं भी किसी भी subclass के लिए visibility खोलता है, एक अलग package में भी — यह उन members के लिए choice का modifier है जिन्हें एक subclass legitimately inherit और उपयोग करने की ज़रूरत है, unrelated code को expose किए बिना।
उदाहरण: The Protected Modifier
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Visibility Control
सबसे tight access level चुनना जो अब भी आपका code काम करने दे एक core defensive-programming habit है: यह आपकी class की internal state को accidental external modification से safe रखता है जबकि अब भी बिल्कुल वे operations expose करता है जो callers को असल में चाहिए।
उदाहरण: Visibility Control
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());
}
}
Login to try C/C++/Java/PHP code in the editor
- एक field को
privateबनाना और इसे किसी दूसरी class से access करना, जो एक compile error है। - यह भूल जाना कि कोई modifier न होने का मतलब है package-private access।
- हर चीज़ को
publicबनाना, जो encapsulation तोड़ता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: