Access modifiers
Access modifiers decide which code can see a class, field, or method. Java has public, protected, package-private (no keyword), and private.
Detailed Explanation
Encapsulation is a rule only if the language enforces it. Access modifiers are that enforcement. private means "this type only". No keyword means "this package only". protected means "this package, plus subclasses even in other packages". public means "everyone".
For top-level classes, only public and package-private exist. A public class must live in a file of the same name. Nested types may use all four. A private nested class is a common way to hide an implementation type (Node, Itr) that should never appear in a public signature.
protected is wider than people remember. A protected member is visible to the whole package, not only to subclasses. A subclass in another package may access the member only through an instance of its own type (or a further subclass), not through a parent-typed variable. That rule exists so you cannot treat protected as public by subclassing in a dummy type and then touching arbitrary parent instances.
Modules add a fifth wall. A public type in a package that the module does not export is invisible to other modules. Reflection can still break in with --add-opens. Treat module exports as part of the published surface.
You reach for private on fields by default, and on helpers that are not the class's job to advertise. You reach for package-private when tests or close collaborators in the same package need a hook you do not want in the public API. You reach for protected only when a designed subclass is supposed to call or override that member. You reach for public when the member is the product.
The cost of everything public is an API you can never shrink. The cost of everything private is tests that cannot see a state you should have made package-private or asserted through behaviour. Prefer testing through the public surface. Open access as a conscious exception, not as a default.
Code Examples
package shop;
public class Item {
private final int paise; // only Item
int stock; // shop package
protected String sku; // shop package + subclasses
public final String name; // everyone
public Item(String name, int paise) {
this.name = name;
this.paise = paise;
}
public int paise() {
return paise;
}
}package shop;
public class Stock {
public static void main(String[] args) {
Item item = new Item("Pen", 20);
item.stock = 5; // same package: legal
System.out.println(item.name + " " + item.stock); // prints: Pen 5
System.out.println(item.paise()); // prints: 20
}
}Common Gotchas
No modifier is not private
every class in the package can read and write that member, including later helpers you have not written yet.
`protected` includes the package
it is not "subclasses only".
A public top-level class must match the file name
`public class Item` in `Stock.java` does not compile.
Private members are invisible to subclasses
a child that declares the same name has a new member, not an override.