Visibility Control or Access Modifiers in Java जावा में विसिबिलिटी कण्ट्रोल या एक्सेस मॉडिफायर

In Java, access modifiers are keywords that determine the visibility or access level of classes, constructors, methods, variables, and data members. By using access modifiers, we can control how these elements are accessed from other classes or packages. Java provides four types of access modifiers:

जावा में एक्सेस मॉडिफायर ऐसे कीवर्ड होते हैं जो क्लास, कांस्ट्रक्टर, वेरिएबल, मेथड या डाटा मेम्बर की एक्सेस (पहुंच) या विसिबिलिटी (दृश्यता) को नियंत्रित करते हैं। इनके माध्यम से हम तय कर सकते हैं कि कौन से भाग किस क्लास या पैकेज से एक्सेस किए जा सकते हैं। जावा में निम्नलिखित चार प्रकार के एक्सेस मॉडिफायर होते हैं:

a) default
b) private
c) protected
d) public


🔸 a) default

When no access modifier is specified, the member has default access. It means the member is accessible only within the same package and not from outside the package.

जब किसी क्लास, मेथड या वेरिएबल के साथ कोई भी एक्सेस मॉडिफायर नहीं दिया जाता है, तब वह default माना जाता है। इसका मतलब है कि वह केवल उसी पैकेज के अंदर उपलब्ध रहता है और बाहर से एक्सेस नहीं किया जा सकता।

✅ Example:

package myWebpage;
class Study {
void message() {
System.out.println("This is a study related with my webpage");
}
}

In the above example, the Study class can be accessed only by classes within the myWebpage package.

इस उदाहरण में Study क्लास को केवल उसी myWebpage पैकेज के अंदर की क्लासेस एक्सेस कर सकती हैं।


🔒 b) private

A member declared with private access can be accessed only within the same class. It is the most restrictive access level.

private एक्सेस मॉडिफायर के साथ डिक्लेयर किया गया मेम्बर केवल उसी क्लास के अंदर एक्सेस किया जा सकता है। यह सबसे सीमित एक्सेस देता है।

❌ Example:

package myPackage;
class Message {
private void show() {
System.out.println("This is a message");
}
}
public class Example {
public static void main(String args[]) {
Message obj = new Message();
obj.show(); // Compile Time Error
}
}

Here, trying to access a private method from another class causes a compile-time error.

यहां दूसरी क्लास से private मेथड को एक्सेस करने की कोशिश करने पर कंपाइल टाइम एरर मिलेगा।


🛡️ c) protected

protected members can be accessed within the same package and by subclasses in different packages using inheritance.

protected एक्सेस लेवल वाले मेथड या वेरिएबल को उसी पैकेज की अन्य क्लासेस और दूसरे पैकेज की सबक्लास द्वारा एक्सेस किया जा सकता है।

📘 Example:

package p1;
public class A {
protected void show() {
System.out.println("this is protected method");
}
}
package p2;
import p1.*;
class B extends A {
public static void main(String args[]) {
B obj = new B();
obj.show(); // works via inheritance
}
}

In this case, class B from a different package can access show() because it extends class A.

इस उदाहरण में, अलग पैकेज की क्लास B, क्लास A को एक्सटेंड करके show() मेथड को एक्सेस कर सकती है।


🌍 d) public

Members declared with public can be accessed from anywhere — across packages and classes.

public के साथ डिक्लेयर किया गया मेम्बर कहीं से भी एक्सेस किया जा सकता है — किसी भी क्लास या पैकेज से।

✅ Example:

package myPackage;
public class Test {
public void show() {
System.out.println("this is Test related with myPackage");
}
}
package yourPackage;
import myPackage.*;
class Example {
public static void main(String args[]) {
Test obj = new Test();
obj.show(); // works everywhere
}
}

Here, Test class and its method show() can be accessed from another package without any issues.

यहां Test क्लास और उसका show() मेथड किसी अन्य पैकेज से आसानी से एक्सेस किया जा सकता है।


✅ Summary Table

Modifier Same Class Same Package Subclass (other package) Other Packages
private       ✅                    ❌                        ❌                                         
default
protected
public

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध