Java is an object-oriented programming language that emphasizes data security and controlled access. One of the key concepts that helps achieve this is Access Modifiers. They define the visibility or accessibility of classes, methods, variables, and constructors in a Java program.
In this blog, we will explore Java Access Modifiers in a simple and practical way, along with diagrams to help you understand clearly.
What are Access Modifiers?
Access Modifiers in Java are keywords used to control the visibility of classes and their members. They determine who can access what in your code.
Java provides four types of access modifiers:
- Default (No Modifier)
- Private
- Protected
- Public
đš Why are Access Modifiers Important?
Access modifiers help in:
- Data hiding (Encapsulation)
- Security of sensitive data
- Controlling unwanted access
- Improving code maintainability
đš Types of Access Modifiers
Letâs understand each modifier in detail.
1ď¸âŁ Default Access Modifier
When no access modifier is specified, it is considered default.
â Accessibility:
- Accessible only within the same package
- Not accessible outside the package
đ Example:
class Demo {
int x = 10; // default access
}
đ Diagram:
Same Package â â Accessible
Different Package â â Not Accessible
2ď¸âŁ Private Access Modifier
The private modifier is the most restrictive.
â Accessibility:
- Accessible only within the same class
- Not accessible outside the class
đ Example:
class Demo {
private int x = 10;
void show() {
System.out.println(x); // accessible here
}
}
đ Diagram:
Same Class â â Accessible
Same Package â â Not Accessible
Different Package â â Not Accessible
đĄ Use Case:
Used for data hiding in encapsulation.
3ď¸âŁ Protected Access Modifier
The protected modifier is slightly more open than the default.
â Accessibility:
- Accessible within the same package
- Accessible in subclasses (even in different packages)
đ Example:
class Parent {
protected int x = 10;
}
class Child extends Parent {
void show() {
System.out.println(x); // accessible
}
}
đ Diagram:
Same Class â â Accessible
Same Package â â Accessible
Different Package â â Not Accessible
Subclass (Other Pkg) â â Accessible
4ď¸âŁ Public Access Modifier
The public modifier is the least restrictive.
â Accessibility:
- Accessible from anywhere
đ Example:
public class Demo {
public int x = 10;
}
đ Diagram:
Same Class â â Accessible
Same Package â â Accessible
Different Package â â Accessible
Anywhere â â Accessible
đš Summary Table
đš Real-Life Analogy
Think of access modifiers like privacy settings in social media:
- Private â Only you can see
- Default â Only friends (same package)
- Protected â Friends + close relatives (subclasses)
- Public â Everyone
đš Important Notes
- You cannot declare a class as private or protected (only nested classes can).
- Top-level classes can be:
- Public
- Default
- Variables, methods, and constructors can use all modifiers.
đš Practical Example
Letâs see a simple program combining all modifiers:
class Example {
private int a = 1;
int b = 2; // default
protected int c = 3;
public int d = 4;
void display() {
System.out.println(a + " " + b + " " + c + " " + d);
}
}
đš When to Use Which?
Use private â Never access data directly
default use â Within the same package
protected â When inheritance comes into the picture
Use public â When data has to be accessible everywhere
đš Best Practices
â Use getters/setters for proper data encapsulation
â Avoid using public unnecessarily
â Use protected only if inheritance is necessary
â Follow proper encapsulation principles
đš Conclusion
Why Use Java Access Modifiers? There are plenty of reasons to use access modifiers while writing secure, maintainable, and well-structured code. This will assist in implementing encapsulation and limiting accessibility of sensitive data.
Knowing the context in which to apply each of these modifiers is one of the essential skills for any Java developer. When you get this concept, your code will be more professional and extensive.
Frequently Asked Questions (FAQs):
1. Java Access Modifiers Explanation
Java Access Modifiers are the keywords used to define the visibility or accessibility of classes, methods, and variables. They assist in managing access or modification of data by various segments of a program.
2. Access Modifiers in Java /* Access modifiers are classified into four types:*/
Java access modifiers have four types:
public â accessible from anywhere
protected â available to the same package and subclasses
default (no modifier) â visible within the same package only
private â available exclusively within the same class
3. Explain the difference between public and private access modifiers.
The least restrictive of the three, and also allows access from any class in any package, is the public modifier. On the other hand, a private modifier allows access only from within the class in which it is declared, making it the most restrictive.
4. What is a protected access modifier in Java, and when should you use it?
Protected Modifier: When do you want to allow access outside the same package and provide access to subclasses of other packages? It is most commonly found in inheritance, where it helps us to share data or methods with child classes.
5. If you have seen the keywords public, private, or any other access modifiers in Java, but do not understand why we would even need them?
Introduction to Access Modifiers: Why are access modifiers important? This enables the filtering of which component has access to which other components in order to keep a clean architecture.
Related Links:
Advantages and Disadvantages of AI
Do visit our channel to know more: SevenMentor
Author:-
Srushti Kulkarni
Srushti Kulkarni
Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.