Access Modifiers in Java
In this lesson: Java access modifiers explained with a visibility table — public, private, protected, default — and examples. In Hindi & English.
What are access modifiers
Access modifiers control who can use a class, field or method. They let you hide internal details and expose only what should be public. Java has four levels, from fully open (public) to fully closed (private). They are the foundation of encapsulation.
The four modifiers
- public — accessible from anywhere.
- private — accessible only within the same class.
- protected — accessible in the same package and by subclasses.
- default (no keyword) — accessible only within the same package.
You choose the most restrictive level that still lets your code work — this keeps data safe.
private and encapsulation
class Account {
private double balance = 0; // hidden data
}
// Account a = new Account();
// a.balance = -500; // ERROR: balance is private
Making balance private stops outside code from changing it directly — for example setting a negative balance. This is encapsulation: the field is hidden and protected.
Getters and setters
class Account {
private double balance = 0;
public double getBalance() { // getter
return balance;
}
public void deposit(double amt) { // controlled setter
if (amt > 0) balance += amt; // validation!
}
}
Public methods give controlled access to a private field. The deposit method validates the amount before changing balance — something you cannot enforce with a public field.
protected and default
- protected — used when subclasses (even in other packages) need access, but the public should not.
- default — written by using no modifier; the member is visible only inside its own package.
These middle levels are less common than public and private, but useful for package and inheritance design.
Quick comparison
| Modifier | Same class | Same package | Subclass | Everywhere |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| default | Yes | Yes | No | No |
| private | Yes | No | No | No |
Common mistakes
- Making all fields public. Keep fields private and expose them via methods.
- Confusing default with public. No modifier means package-private, not public.
- Overusing protected. Use it only when subclasses genuinely need access.
- Skipping validation in setters. The point of a setter is to check values before setting.
Summary
- Access modifiers control who can use a class member.
publicis open everywhere;privateis same-class only.protectedadds subclasses; default is package-only.- Keep fields private and expose them through getters/setters — encapsulation.
- Setters can validate values before changing private data.
Next, revise data hiding with classes and objects, or learn the static keyword.
Frequently Asked Questions
What are access modifiers in Java?
public (everywhere), private (same class only), protected (same package plus subclasses), and default (same package). They protect your code.What is the difference between public and private?
public means the member can be accessed from anywhere. private means only the same class can access it. Fields are usually private, and public methods control access to them.What is encapsulation?
What is the default access modifier?
What is a getter and setter?
getName()), and a setter is a public method that changes it (like setName()). They give controlled access to private data.