Problem Statement
Show visibility control (public/private/protected) with a short example.
Explanation
public is accessible anywhere, protected is only inside the class and subclasses, private is only inside the class. Use protected for extension points and private to hide internals.
Code Solution
SolutionRead Only
class Account {
public id: string;
protected balance = 0;
private pin = 1234;
constructor(id: string){ this.id = id; }
deposit(amount: number){ this.balance += amount; }
}
class Savings extends Account {
accrue(){ this.balance += 1; /* ok: protected */ }
// this.pin; // ❌ private: not accessible here
}
const a = new Savings('A1');
a.id; // ✅ public
// a.balance // ❌ protected
// a.pin // ❌ private