Every Java developer writes classes, methods, and fields. But who should be allowed to use them? Access modifiers answer exactly that — they control visibility, enforce encapsulation, and form the foundation of clean API design.
Java has four:
- Default (Package-private)
- Private
- Protected
- Public
Let's break each one down.
Default Access Modifier
If no access modifier is specified, the member has default (package-private) access and can only be accessed within the same package.This means only classes within the same package can access it.
Example
class Car {
String model; // default access
}
public class Main {
public static void main(String[] args){
Car c = new Car();
c.model = "Tesla"; // accessible within the same package
System.out.println(c.model);
}
}
Output
Private Access Modifier
The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared.
Example
class Person {
// private variable
private String name;
public void setName(String name) {
this.name = name; // accessible within class
}
public String getName() { return name; }
}
public class Geeks {
public static void main(String[] args)
{
Person p = new Person();
p.setName("Alice");
// System.out.println(p.name); // Error: 'name'
// has private access
System.out.println(p.getName());
}
}
Output
Protected Access Modifier
The protected access modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages.
Example
// File: Vehicle.java in package p1
package p1;
public class Vehicle {
protected int speed;
}
// File: Bike.java in package p2
package p2;
import p1.Vehicle;
public class Bike extends Vehicle {
void showSpeed() {
speed = 100; // allowed: subclass in different package
System.out.println(speed);
}
}
// File: Test.java in package p2
package p2;
import p1.Vehicle;
public class Test {
public static void main(String[] args) {
Bike b = new Bike();
b.showSpeed(); // prints 100
Vehicle v = new Vehicle();
// System.out.println(v.speed); // ERROR: cannot access protected outside package & non-subclass
}
}
Output
Public Access Modifier
The public access modifier is specified using the keyword public. Public members are accessible from everywhere in the program. There is no restriction on the scope of public data members.
Example
class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.add(5, 10)); // accessible anywhere
}
}
Output
Private:
The idea should be use as restrictive access as possible, so private should be used as much as possible.
Default (Package-Private):
Often used in package-scoped utilities or helper classes.
Protected:
Commonly used in inheritance-based designs like framework extensions.
Public:
This is used for API endpoints, service classes, or utility methods shared across different parts of an application.





Top comments (0)