ProgramGuru

public, private, and protected

Understand public, private, and protected

public members are accessible wherever the object is visible; private members are limited to their declaring class; protected members are accessible within the class and its subclasses.

What you will learn: the purpose of public, private, and protected, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how public, private, and protected works and where its type rules apply.
  • Predict the exact output of the public, private, and protected example.
  • Diagnose a common issue: Do not read teacher.

Build the mental model

These are TypeScript access-control rules, distinct from JavaScript #private runtime semantics. A subclass can use a protected base member, but outside code cannot access that member through the instance in checked TypeScript.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

public, private, and protected: explained visuallyPerson: protected → Teacher extends → Subclass may readPerson: protectedTeacher extendsSubclass may readFollow the values through the operation
public, private, and protected: Person: protected → Teacher extends → Subclass may read.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

class Person {
  constructor(public name: string, protected prefix: string) {}
  label(): string { return this.prefix + this.name; }
}
class Teacher extends Person {
  describe(): string { return `Teacher: ${this.prefix}${this.name}`; }
}
const teacher = new Teacher("Mira", "Dr. ");
console.log(teacher.label());
console.log(teacher.describe());

Verified output

Dr. Mira
Teacher: Dr. Mira

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

Person initializes name and protected prefix. Teacher inherits label and can use prefix in its own describe method. Both lines include Dr. Mira, but each has different surrounding text.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not read teacher.prefix directly outside the class hierarchy; protected allows subclass access, not arbitrary client access.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Add a private numeric id to Person and try to use it from Teacher; observe the access error.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Which code may directly read a protected prefix member?
Choose one answer. You can try again.

Answer and explanation

Methods in the declaring class and its subclasses.

public members are accessible wherever the object is visible; private members are limited to their declaring class; protected members are accessible within the class and its subclasses.

Review the key idea

public, private, and protected: public members are accessible wherever the object is visible; private members are limited to their declaring class; protected members are accessible within the.

Person initializes name and protected prefix. Teacher inherits label and can use prefix in its own describe method. Both lines include Dr. Mira, but each has different surrounding text.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Readonly Class Properties →