Access modifiers in Java control the visibility of classes, constructors, methods, and fields. They help implement encapsulation by restricting access to different parts of a program.
- Public modifier accessible from anywhere in the program
- Protected modifier accessible within the same package and by subclasses
- Private modifier accessible only within the same class
- Default modifier accessible only within the same package
Real-World Example: In a banking app, private is used for balance (hidden), default for internal helpers, protected for methods used in subclasses, and public for actions like deposit or view details.
Types of Access Modifiers
Java provides four access levels, each with different rules for accessing classes and their members.
1. Private Access Modifier
The private modifier restricts access to members within their enclosing class. It is commonly used to protect fields and internal implementation details.
class Person{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
public class Geeks{
public static void main(String[] args){
Person p = new Person();
p.setName("Alice");
// Access private field using getter
System.out.println(p.getName());
// System.out.println(p.name); // Compile-time error
}
}
Output
Alice
Explanation: The name field cannot be accessed directly from the Geeks class because it is private. The public getter and setter methods provide access to the field.
2. Default Access Modifier (Package-Private)
When no access modifier is specified, a class or member has package-private access. It can be accessed by other classes within the same package.
class Car{
// Package-private variable
String model;
}
public class Geeks{
public static void main(String[] args){
Car c = new Car();
c.model = "Tesla";
System.out.println(c.model);
}
}
Output
Tesla
Explanation: The model variable has package-private access. Since Car and Geeks belong to the same package, Geeks can access it directly.
Note: default is not an access modifier keyword. Package-private access is applied when no access modifier is specified.
3. Protected Access Modifier
The protected modifier allows access within the same package and from subclasses in other packages, subject to Java's protected-access rules.
File: Vehicle.java (package p1)
package p1;
public class Vehicle{
protected int speed = 100;
}
File: Bike.java (package p2)
package p2;
import p1.Vehicle;
public class Bike extends Vehicle{
public void showSpeed(){
// Access inherited protected field
System.out.println("Speed: " + speed);
}
}
File: Test.java (package p2)
package p2;
public class Test{
public static void main(String[] args){
Bike b = new Bike();
b.showSpeed();
}
}
Output
Speed: 100
Explanation: Bike extends Vehicle and accesses the inherited protected field speed through its own method, even though the classes belong to different packages.
Note: A subclass in another package can access an inherited protected instance member through this, super, or a qualifying reference whose type is the accessing subclass or one of its subclasses. It cannot access that member through an arbitrary superclass reference. Top-level classes cannot be declared protected.
4. Public Access Modifier
The public modifier provides the widest accessibility. Public members can be accessed from other classes wherever their declaring type is accessible.
class MathUtils{
public static int add(int a, int b){
return a + b;
}
}
public class Geeks{
public static void main(String[] args){
System.out.println(MathUtils.add(5, 10));
}
}
Output
15
Explanation: The add() method is declared public, allowing the Geeks class to access it through MathUtils.
Note: Public accessibility depends on the visibility of the declaring type and Java module access rules, including package exports. Top-level classes and interfaces can be declared public or package-private, but not private or protected.
Comparison Table of Access Modifiers in Java
Access Location | private | Default | protected | public |
|---|---|---|---|---|
Same class | Yes | Yes | Yes | Yes |
Other classes in the same package | No | Yes | Yes | Yes |
Subclass in a different package | No | No | Yes | Yes |
Non-subclass in a different package | No | No | No | Yes |
Notes:
- Private members are also accessible under Java's nested-class and nestmate access rules.
- Protected access across packages is restricted. A subclass can access an inherited protected instance member through this, super, or a reference whose qualifying type is that subclass or a subclass of it.
- The table assumes the declaring type is accessible. Java module boundaries may impose additional restrictions.