Bốn mức access, từ chặt nhất → rộng nhất:
| Modifier | Cùng class | Cùng package | Subclass (khác package) | Mọi nơi |
|---|---|---|---|---|
private | ✅ | ❌ | ❌ | ❌ |
| default (không có) | ✅ | ✅ | ❌ | ❌ |
protected | ✅ | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ | ✅ |
Top-level class chỉ dùng được public hoặc default. Field/method/nested class: cả 4.
java
public class Account {
private final String id; // ẩn hoàn toàn
protected BigDecimal balance; // subclass dùng được
String accountType; // default — package-private
public String getId() { return id; }
}Nguyên tắc: chọn modifier hạn chế nhất đủ dùng — mở càng rộng càng khó refactor.
Java 9+ (JPMS): "public" còn cần module exports package đó để cross-module visible.
Four levels of access, strictest → widest:
| Modifier | Same class | Same package | Subclass (other package) | Anywhere |
|---|---|---|---|---|
private | ✅ | ❌ | ❌ | ❌ |
| default (none) | ✅ | ✅ | ❌ | ❌ |
protected | ✅ | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ | ✅ |
Top-level classes accept only public or default. Fields/methods/nested classes accept all four.
java
public class Account {
private final String id; // fully hidden
protected BigDecimal balance; // subclasses can see it
String accountType; // default — package-private
public String getId() { return id; }
}Rule: pick the most restrictive modifier that still works — wider is harder to refactor later.
Java 9+ (JPMS): even public requires the module to exports the package for cross-module visibility.