Java tutorials > Core Java Fundamentals > Object-Oriented Programming (OOP) > What are access modifiers and their scope?
What are access modifiers and their scope?
Overview of Access Modifiers
Public Access Modifier
public
access modifier grants the widest level of accessibility. Any class, method, or variable declared as public
can be accessed from any other class, regardless of the package they belong to.
public class PublicClass {
public int publicVariable = 10;
public void publicMethod() {
System.out.println("This is a public method.");
}
}
Protected Access Modifier
protected
access modifier provides access within the same package and to subclasses (derived classes) even if they are in a different package. If a subclass tries to access a protected member of its superclass through an instance of the superclass, rather than through inheritance (i.e. using the 'this' keyword, or an instance of itself), it will not be allowed unless they are in the same package.
package package1;
public class ProtectedClass {
protected int protectedVariable = 20;
protected void protectedMethod() {
System.out.println("This is a protected method.");
}
}
// In a different package
package package2;
import package1.ProtectedClass;
class SubClass extends ProtectedClass {
public void accessProtected() {
System.out.println("Protected Variable: " + protectedVariable);
protectedMethod();
}
}
Default (Package-Private) Access Modifier
package package1;
class DefaultClass {
int defaultVariable = 30; // No access modifier specified
void defaultMethod() { // No access modifier specified
System.out.println("This is a default method.");
}
}
//In the same package
package package1;
class AnotherClass {
public void accessDefault() {
DefaultClass obj = new DefaultClass();
System.out.println("Default Variable: " + obj.defaultVariable);
obj.defaultMethod();
}
}
Private Access Modifier
private
access modifier provides the strictest level of access control. A class, method, or variable declared as private
is only accessible within the class in which it is declared.
public class PrivateClass {
private int privateVariable = 40;
private void privateMethod() {
System.out.println("This is a private method.");
}
public void accessPrivate() {
System.out.println("Private Variable: " + privateVariable);
privateMethod();
}
}
// Attempting to access from another class will result in a compilation error
public class AnotherClass {
public void accessPrivate() {
PrivateClass obj = new PrivateClass();
// System.out.println("Private Variable: " + obj.privateVariable); // Compilation error
// obj.privateMethod(); // Compilation error
}
}
Scope Summary
Modifier
Class
Package
Subclass
World
public
Yes
Yes
Yes
Yes
protected
Yes
Yes
Yes
No
default (package-private)
Yes
Yes
No
No
private
Yes
No
No
No
Concepts Behind the Snippet
Real-Life Use Case Section
BankAccount
class. The account balance should be private
to prevent direct access and modification from outside the class. public
methods like deposit()
and withdraw()
provide controlled access to modify the balance. A protected
method might be used in a subclass to handle overdrafts.
Best Practices
private
whenever possible to prevent direct access and modification.public
fields; instead, provide getter and setter methods for controlled access.protected
and default
access modifiers.
Interview Tip
When to Use Them
public
when you want members to be accessible from anywhere.protected
when you want members to be accessible within the package and by subclasses.default
(package-private) when you want members to be accessible only within the same package.private
when you want members to be accessible only within the class.
Memory Footprint
Alternatives
Pros
Cons
FAQ
-
What happens if I don't specify an access modifier?
If you don't specify an access modifier, the default (package-private) access modifier is applied. This means the member is only accessible within the same package. -
Can I change the access modifier of a method in a subclass?
You can increase the visibility of a method in a subclass (e.g., change fromprotected
topublic
), but you cannot decrease it (e.g., change frompublic
toprivate
). This is due to the Liskov Substitution Principle. -
Why should I use private instance variables?
Using private instance variables promotes encapsulation. It prevents direct access to the internal state of the object from outside the class, allowing you to control how the state is modified through getter and setter methods. This helps maintain data integrity and allows for future modifications without breaking external code.