Sub topic 6
Controlling Access: `public`, `private`, and `protected`
Prerequisites: basic TypeScript types, JavaScript classes, and the this keyword. This chapter assumes you know how to create an object with new.
An access modifier controls where a class member can be used. A member can be a field or a method. TypeScript checks these rules while compiling your code.
| Modifier | Inside declaring class | Subclasses | Other code |
|---|---|---|---|
| `public` | Yes | Yes | Yes |
| `private` | Yes | No | No |
| `protected` | Yes | Yes | No |
`public`: the default visibility
A public member is part of the class's external API. Other code can read or change it. Members are public by default, but writing the modifier makes the intent clear.
`private`: hide implementation details
A private member can only be accessed inside the class that declares it. Use it for state or helper methods that callers should not manipulate directly.
class Account {
public readonly id: string;
private balance: number;
constructor(id: string, initialBalance: number) {
this.id = id;
this.balance = initialBalance;
}
public deposit(amount: number): void {
if (amount <= 0) {
throw new Error("Amount must be positive");
}
this.balance += amount;
}
public getBalance(): number {
return this.balance;
}
private logChange(): void {
console.log(`Account ${this.id} changed`);
}
}
const account = new Account("a-1", 100);
account.deposit(50);
console.log(account.id); // a-1
console.log(account.getBalance()); // 150
// account.balance = 0;
// Error: Property 'balance' is private.The balance field is private, so callers must use methods that preserve the class's rules. The id field is public but readonly, so callers can read it but cannot assign a new value after construction.
`protected`: share with subclasses
A protected member is private to outside code but available inside the declaring class and its subclasses. It is useful when a base class provides state or behavior that derived classes must customize.
class Employee {
constructor(
public name: string,
protected salary: number
) {}
protected bonus(): number {
return this.salary * 0.1;
}
}
class Manager extends Employee {
totalPay(): number {
return this.salary + this.bonus();
}
}
const manager = new Manager("Ada", 100000);
console.log(manager.totalPay()); // 110000
// manager.salary; // Error: 'salary' is protected.
// manager.bonus(); // Error: 'bonus' is protected.External code can use totalPay, but it cannot access the implementation details used to calculate it.
Choosing a modifier
Start with the narrowest useful visibility:
- Use
privatefor details that belong only to one class. - Use
protectedonly when subclasses genuinely need direct access. - Use
publicfor stable operations and data that callers are meant to use.
Exercise
Which modifier should you use for a field that callers must not change, but subclasses must read?
Show answer
Use protected. If subclasses do not need direct access, prefer private and expose a controlled method or getter.