What are Access Modifiers in Java?
Access Modifiers in Java are keywords that set the visibility (access level) of classes, variables, methods, and constructors.
They determine which other classes can access a member (field/method) of a class.
Types of Access Modifiers in Java
Modifier | Class | Package | Subclass | World (Global) |
---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ |
protected |
✅ | ✅ | ✅ | ❌ |
(default)* | ✅ | ✅ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ |
Default = No modifier (also called package-private)
1. public
Accessible from anywhere (inside any class, package, or project).
public class PublicExample {
public String message = "I am public";
public void showMessage() {
System.out.println(message);
}
}
// Another class, even in another package
public class Test {
public static void main(String[] args) {
PublicExample obj = new PublicExample();
obj.showMessage(); // ✅ Accessible
}
}
2. private
Accessible only within the same class.
public class PrivateExample {
private int age = 25;
private void showAge() {
System.out.println("Age: " + age);
}
public void accessPrivate() {
showAge(); // ✅ Valid: accessed within the same class
}
}
// Another class
public class Test {
public static void main(String[] args) {
PrivateExample obj = new PrivateExample();
// obj.age; ❌ Not accessible
// obj.showAge(); ❌ Not accessible
obj.accessPrivate(); // ✅ Indirect access via public method
}
}
3. default
(No Modifier)
Accessible only within the same package.
class DefaultExample { // No `public` keyword = package-private
void greet() {
System.out.println("Hello from default!");
}
}
// In the same package
public class Test {
public static void main(String[] args) {
DefaultExample obj = new DefaultExample();
obj.greet(); // ✅ Accessible
}
}
// In a different package
// ❌ Will cause compile error: class DefaultExample is not public
4. protected
Accessible within the same package and in subclasses in other packages.
public class ProtectedExample {
protected void show() {
System.out.println("Protected method accessed");
}
}
// Subclass in same or different package
public class SubClass extends ProtectedExample {
public void display() {
show(); // ✅ Accessible in subclass
}
}
public class Test {
public static void main(String[] args) {
SubClass obj = new SubClass();
obj.display(); // ✅ Works
// obj.show(); ❌ Not accessible directly from non-subclass
}
}
Summary Table
Access Modifier | Accessible Within Class | Same Package | Subclass (Different Package) | Anywhere |
---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ |
protected |
✅ | ✅ | ✅ | ❌ |
default |
✅ | ✅ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ |
Best Practices
- Use
private
for encapsulation (e.g., fields). - Use
public
for APIs and methods that must be accessed globally. - Use
protected
for reusable methods in subclassed components. - Use
default
when access should be limited within the package.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.