Advertisement
Beginner TypeScript, Classes: Access Modifiers, Abstract Classes & Implements

What do the public, private, and protected access modifiers control on a class member?

Short answer

public (the default) makes a member accessible from anywhere; private restricts access to only within the declaring class itself; protected allows access within the declaring class AND its subclasses, but not from outside code.

Save TypeScript mock interview
Advertisement
AModel answer
public (the default) makes a member accessible from anywhere; private restricts access to only within the declaring class itself; protected allows access within the declaring class AND its subclasses, but not from outside code.
class Account {
  public balance: number = 0;
  private pin: string = '1234';
  protected accountType: string = 'checking';
}
Real-world example
Hiding an internal PIN or password field so only the class's own methods can read it.
Likely follow-ups
  • Are these modifiers enforced at runtime, or only during compilation?
Related topics