Simulator
Encapsulation Lab
Inspect public, private, protected, and readonly members. Try a direct access and see whether encapsulation blocks it.
balance
Hidden representation — callers must use deposit/withdraw.
bank-account.ts
class BankAccount {
constructor(
public readonly id: string,
protected ownerName: string,
private balance: number
) {}
getBalance() { return this.balance; }
deposit(amount: number) {
if (amount <= 0) throw new Error("invalid deposit");
this.balance += amount;
}
withdraw(amount: number) {
if (amount > this.balance) throw new Error("insufficient funds");
this.balance -= amount;
}
}