When learning Java, one of the most important concepts you will encounter is Java Access Modifiers. These modifiers play a crucial role in controlling the visibility and accessibility of classes, methods, and variables in a Java program.
Understanding access modifiers in Java is essential for writing secure, maintainable, and well-structured code. Whether you're a beginner or an experienced developer, mastering this concept will help you design better applications.
In this guide, we will explore Java access modifiers, their types, real-world examples, and best practices in a simple and easy-to-understand way.
What are Java Access Modifiers?
Definition
Java Access Modifiers are keywords used to define the accessibility or scope of a class, method, or variable. They determine who can access what within a program.
Access modifiers in Java are keywords used to define the visibility or scope of classes, methods, variables, and constructors.
They control how different parts of a program interact with each other, ensuring proper data protection and encapsulation.
Why are Access Modifiers Important?
Ensure data security
Prevent unauthorized access
Improve code organization
Support encapsulation (OOP principle)
Types of Access Modifiers in Java
There are four main types of access modifiers in Java:
Public
Private
Protected
Default (No Modifier)
Letβs understand each one in detail.
1. Public Access Modifier
Overview
The public access modifier allows access from anywhere in the program. It has the widest accessibility.
Key Features
Accessible from any class
Accessible across packages
No restrictions
Example
public class Student {
public String name = "John";
public void display() {
System.out.println(name);
}
}
When to Use Public
Use public when you want methods or variables to be accessible globally.
2. Private Access Modifier
Overview
The private access modifier restricts access to the same class only.
Key Features
Not accessible outside the class
Ensures data hiding
Supports encapsulation
Example
class Student {
private int age = 20;
public int getAge() {
return age;
}
}
The private modifier provides the highest level of restriction.
Commonly used for variables and sensitive data.
When to Use Private
Use private to protect sensitive data and prevent direct access.
3. Protected Access Modifier
Overview
The protected access modifier allows access within the same package and by subclasses.
Key Features
Accessible within the package
Accessible in subclasses (even in different packages)
Example
class Animal {
protected void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
- void bark() {*
- sound();*
- }*
}
When to Use Protected
Use protected when working with inheritance.
4. Default Access Modifier (No Modifier)
Overview
If no modifier is specified, Java uses the default access modifier.
Key Features
Accessible only within the same package
Not accessible outside the package
Example
class Demo {
void show() {
System.out.println("Default access");
}
}
When to Use Default
Use the default when you want limited access within the same package.
Access Modifiers in Java: Comparison Table
π Diagram:
Same Package β β Accessible
Different Package β β Not Accessible
Same Class β β Accessible
Same Package β β Not Accessible
Different Package β β Not Accessible
Same Class β β Accessible
Same Package β β Accessible
Different Package β β Not Accessible
Subclass (Other Pkg) β β Accessible
Same Class β β Accessible
Same Package β β Accessible
Different Package β β Accessible
Anywhere β β Accessible
Real-World Example of Java Access Modifiers
Imagine a banking application:
Private β Account balance (hidden)
Public β Methods to deposit/withdraw money
Protected β Access for subclass accounts
Default β Internal operations within the package
This structure ensures security and proper data handling.
Consider a banking application:
Account balance β private
Branch details β Default
Manager operations β protected
Customer services β public
This layered approach ensures data security, controlled access, and proper system design.
Benefits of Using Java Access Modifiers
1. Data Security
Protect sensitive data from unauthorized access.
2. Encapsulation
Encapsulation is a core OOP concept supported by access modifiers.
3. Code Maintainability
Helps organize code effectively.
4. Controlled Access
Restricts unnecessary exposure of data.
Best Practices for Using Access Modifiers
1. Use Private by Default
Keep variables private unless necessary.
2. Use Public Carefully
Avoid exposing everything publicly.
3. Prefer Getter and Setter Methods
Control access through methods.
4. Use Protected for Inheritance
Helpful in subclass relationships.
Provide access via getters and setters
Follow the Principle of Least Privilege
Common Mistakes to Avoid
1. Overusing Public
Makes code less secure.
2. Ignoring Encapsulation
Direct access to variables can cause issues.
3. Wrong Modifier Selection
Choosing incorrect modifiers leads to bugs.
Confusing default and protected access
Using incorrect access levels in large-scale applications
Java Access Modifiers and OOP Concepts
Access modifiers are closely related to Object-Oriented Programming (OOP):
Encapsulation β Data hiding using
Aarti Choudhary
Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.
All articles by Aarti Choudhary