Access Modifiers
Access modifiers decide who can touch a class member: everyone, only the class, or the class and its children. They are how you protect an object’s internal state.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Use public, private and protected
- Compare TypeScript private with JavaScript #private
- Combine modifiers with readonly and static
- Design classes with a small public surface
Explanation#
public (the default): accessible from anywhere.
private: accessible only inside the same class. protected: inside the class and its subclasses.
TypeScript’s private is a compile-time check only — at runtime the property is a normal one. JavaScript’s #field is enforced by the engine at runtime. Use #field when real privacy matters.
readonly can be combined with any modifier (private readonly). static members belong to the class itself: Config.defaultTimeout.
Syntax#
class A {
public x = 1;
protected y = 2;
private z = 3;
#w = 4; // runtime-private
static count = 0;
}
Code example: Who can access what?#
class Employee {
static company = 'Techie Babaji';
public name: string;
protected salary: number;
private pan: string;
#bankPin: number;
constructor(name: string, salary: number, pan: string, pin: number) {
this.name = name;
this.salary = salary;
this.pan = pan;
this.#bankPin = pin;
}
}
class Manager extends Employee {
bonus() {
return this.salary * 0.2; // ✓ protected is visible in subclasses
// return this.pan; // ✗ Property 'pan' is private and only accessible within class 'Employee'
}
}
const m = new Manager('Asha', 100000, 'ABCDE1234F', 4321);
m.name; // ✓ public
m.salary; // ✗ Property 'salary' is protected and only accessible within class 'Employee' and its subclasses
Employee.company; // ✓ static, accessed on the class
console.log((m as any).pan); // 'ABCDE1234F' — TS private is only a compile-time check
// (m as any).#bankPin // ✗ impossible even at runtime
TypeScript rejects m.salary and this.pan. Casting to any bypasses TS private (it prints 'ABCDE1234F'), but #bankPin cannot be reached at all.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- static company belongs to the class, not each employee.
- Manager can read salary because it is protected.
- pan is private to Employee — even subclasses cannot see it.
- Outside code can only use public members.
- as any skips type checking and exposes the TS-private field at runtime.
- #bankPin stays private at runtime too.
Real-world use case#
A payment service keeps API keys and internal caches private, exposes a few public methods, and lets specialised subclasses use protected helpers.
Common mistakes#
TS private disappears after compilation. Never treat it as protection for secrets — they should not be in client code at all.
A large public surface means more code depends on internals, so changing them breaks others.
Protected members become part of the contract with every subclass. Prefer private plus small protected hooks.
Best practices#
- Default to private; make public only what callers need
- Use #private when runtime privacy matters
- Use protected sparingly
- Use private readonly for injected dependencies
Remember this#
public = everyone. protected = family (class + children). private = only me.
private = compile-time lock. # = runtime lock.
static = belongs to the class, not the objects.
Practice exercise#
Your task: In class Car, make engineNumber private readonly, speed protected and model public. Add a static wheels = 4.
Need a hint?
Show solution
class Car {
static wheels = 4;
constructor(
public model: string,
protected speed: number,
private readonly engineNumber: string,
) {}
}
Mini quiz#
Which modifier allows access from subclasses but not from outside?
Want more? Practise all TypeScript MCQs · TypeScript interview questions
My notes#
Saved in this browser.