
4 Access Modifiers in Java: A Helpful Guide with Examples
What are Access Modifiers in Java?
The access modifiers in Java are keywords that determine the visibility and accessibility of classes, methods, and variables. The access modifiers in Java help in encapsulating data and controlling how different parts of a program interact with each other. Java provides four types of access modifiers:
- Private
- Default (Package-Private)
- Protected
- Public
Each modifier has different access levels. Let’s explore each of them in detail with examples.
Private Access Modifier
The private modifier restricts access to members (variables and methods) within the same class. It means that private members cannot be accessed outside their defining class.
Example:
class Example { private int privateVar = 10; private void display() { System.out.println("Private method called: " + privateVar); } public void accessPrivate() { display(); // Can be accessed within the class } } public class Test { public static void main(String[] args) { Example obj = new Example(); // obj.privateVar = 20; // Error: privateVar has private access // obj.display(); // Error: display() has private access obj.accessPrivate(); // Allowed, since it's a public method } }
Key Points:
- Private members are accessible only within the same class.
- They are not inherited by subclasses.
- Useful for data hiding and encapsulation.
Default (Package-Private) Access Modifier
If no access modifier is specified, the default access level is applied. This means that the member is accessible only within the same package.
Example:
class DefaultExample { int defaultVar = 30; // No modifier means default access void display() { System.out.println("Default method called: " + defaultVar); } } public class Test { public static void main(String[] args) { DefaultExample obj = new DefaultExample(); obj.defaultVar = 40; // Allowed since it's in the same package obj.display(); // Allowed } }
Key Points:
- Default members are accessible only within the same package.
- Cannot be accessed from outside the package.
- Useful when designing package-level functionality.
Protected Access Modifier
The protected modifier allows access within the same package and by subclasses (even if they are in different packages).
Example:
class Parent { protected int protectedVar = 50; protected void display() { System.out.println("Protected method called: " + protectedVar); } } class Child extends Parent { void show() { display(); // Accessible in subclass System.out.println("Accessing protected variable: " + protectedVar); } } public class Test { public static void main(String[] args) { Child obj = new Child(); obj.show(); // Works fine // obj.protectedVar = 60; // Accessible since Test is in the same package } }
Key Points:
- Accessible within the same package.
- Accessible by subclasses, even in different packages via inheritance.
- Useful for extending classes while keeping certain members protected.
Public Access Modifier
The public modifier allows unrestricted access from anywhere in the program.
Example:
class PublicExample { public int publicVar = 70; public void display() { System.out.println("Public method called: " + publicVar); } } public class Test { public static void main(String[] args) { PublicExample obj = new PublicExample(); obj.publicVar = 80; // Allowed obj.display(); // Allowed from anywhere } }
Key Points:
- Accessible from anywhere in the project.
- Suitable for APIs and classes that need to be accessed globally.
Summary Table for Access Modifiers in Java
Modifier | Same Class | Same Package | Subclass (Different Package) | Other Packages |
---|---|---|---|---|
Private | ✅ Yes | ❌ No | ❌ No | ❌ No |
Default | ✅ Yes | ✅ Yes | ❌ No | ❌ No |
Protected | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
Public | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Choosing the Right Access Modifier
When deciding which access modifier to use, consider the following:
- Use private for sensitive data and helper methods that should not be exposed.
- Use default when you want access within a package but not outside.
- Use protected when you want to allow access to subclasses but not to unrelated classes.
- Use public for APIs, utility classes, or methods meant for widespread use.
By understanding and properly using access modifiers, you can write better-structured, secure, and maintainable Java programs.
You also need to know that Access Modifiers in Java and Access Specifiers are often used interchangeably, but technically, Java only has Access Modifiers.
Why the Confusion?
- Some older Java references used “Access Specifiers” as a term.
- The official Java documentation only uses Access Modifiers.
- Some developers use “Access Specifiers” to mean any visibility-related keywords (like
final
,static
, etc.), but this is not correct.
Conclusion
In Java, Access Modifiers (private, default, protected, public) control access to classes, methods, and variables. The term Access Specifiers is not officially recognized in Java.
If you like this article, please comment and share. For more reads, please visit my blog.
About the author : Mohit Jain
I have more than five years of work experience as a Java Developer. I am passionate about teaching and learning new technologies. I specialize in Java 8, Hibernate, Spring Framework, Spring Boot, and databases like MySQL, and Oracle. I like to share my knowledge with others in the form of articles.
Great job 👍 keep sharing the knowledge
Thank you, Sai. I am happy to see that you find this article helpful.