Part 08 / Topic 3 of 12
Encapsulation and access modifiers
Encapsulation means hiding a class's fields and letting outside code change them only through methods that can check the change first. A BankAccount that lets anyone write acc.balance = 999999999; has no rules left to enforce; a BankAccount whose balance is private can only be changed by calling deposit(...) or withdraw(...), which can refuse bad values.
Think of a bank counter with a glass partition: you hand the clerk a deposit slip (a method call); you do not reach behind the counter and move the cash yourself (direct field access).
| Modifier | Visible from | Typical use |
|---|---|---|
private |
the same class only | fields, and helper methods only the class itself needs |
| (none — package-private) | the same package | classes cooperating tightly inside one package, rarely fields |
protected |
the same package, plus subclasses anywhere | fields and methods a subclass needs to extend (section 6) |
public |
anywhere | the class's actual API: its constructors and the methods callers need |
Real-world example: a BankAccount that only changes its balance through checked methods.
static class BankAccount {
private final String accountNumber; // fixed after construction
private double balance; // never touched directly from outside
BankAccount(String accountNumber, double openingBalance) {
if (openingBalance < 0) {
throw new IllegalArgumentException("opening balance cannot be negative: " + openingBalance);
}
this.accountNumber = accountNumber;
this.balance = openingBalance;
}
String accountNumber() { return accountNumber; }
double balance() { return balance; }
void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("deposit must be positive: " + amount);
balance += amount;
}
void withdraw(double amount) {
if (amount <= 0) throw new IllegalArgumentException("withdrawal must be positive: " + amount);
if (amount > balance) throw new IllegalStateException("insufficient balance: has ₹" + balance + ", wants ₹" + amount);
balance -= amount;
}
}
SBIN0001234 balance: ₹5000.0
after deposit(1500): ₹6500.0
after withdraw(2000): ₹4500.0
rejected: insufficient balance: has ₹4500.0, wants ₹100000.0
rejected: deposit must be positive: -50.0
accountNumber is also final: once the constructor sets it, no method in the class can change it again, which matches reality — a bank account's number never changes for the life of the account. accountNumber() and balance() are plain methods that return a field, called accessors; Java has no built-in "property" syntax like some other languages, so this methodName() style (no get prefix needed, though you will see getX() plenty in other people's code) is how you read a private field from outside the class.
🔍 Going deeper: package-private (writing no modifier at all) is easy to reach for by accident — just forget the keyword — so always write
privateorpublicon purpose, never by omission.protectedis covered properly once inheritance exists, in section 6.
Full program: examples/Encapsulation.java.
Done here? The practice set for Part 8 — including the solutions — is on the part page.