List Java's four access levels and who can see a member at each level.
Show the explanation
| Modifier | Same class | Same package | Subclass in another package | Everyone |
|---|---|---|---|---|
private | ✔ | |||
| (none) "package-private" | ✔ | ✔ | ||
protected | ✔ | ✔ | ✔ (through inheritance) | |
public | ✔ | ✔ | ✔ | ✔ |
package bank;
public class Account {
private long balance; // only Account
long branchCode; // anything in package bank
protected void audit() { } // bank + subclasses elsewhere
public long getBalance() { return balance; }
}Top-level classes can only be public or package-private. A sensible default is to make fields private and give everything the narrowest access that works. Note that protected is wider than package-private, not narrower — a common slip.
Original UniCircle question. Last reviewed 22 Oct 2026.
