0
0
Typescriptprogramming~10 mins

Access modifiers public private protected in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Access modifiers public private protected
Start: Define class with members
Apply access modifiers
public
Accessible everywhere
Accessible only inside class
Accessible inside class and subclasses
Use members accordingly
End
This flow shows how class members are marked with public, private, or protected and how their accessibility differs.
Execution Sample
Typescript
class Person {
  public name: string;
  private age: number;
  protected city: string;

  constructor(name: string, age: number, city: string) {
    this.name = name; this.age = age; this.city = city;
  }
}
Defines a class with three members having different access modifiers.
Execution Table
StepActionMemberAccess ModifierAccessible FromResult
1Define membernamepublicAnywhereCan be accessed from any code
2Define memberageprivateInside Person class onlyCannot be accessed outside Person
3Define membercityprotectedPerson and subclassesAccessible in Person and subclasses
4Create instance p = new Person('Alice', 30, 'NY')---Instance created with values
5Access p.namenamepublicAnywhereAllowed, returns 'Alice'
6Access p.ageageprivateOutside classError: Property 'age' is private
7Access p.citycityprotectedOutside classError: Property 'city' is protected
8Subclass Employee extends Person---Subclass created
9Inside Employee method, access this.citycityprotectedSubclassAllowed, can access 'city'
10Inside Employee method, access this.ageageprivateSubclassError: 'age' is private to Person
11End---Execution ends
💡 Execution ends after demonstrating access rules for all modifiers
Variable Tracker
VariableStartAfter Instance CreationAfter Access AttemptsFinal
nameundefined'Alice''Alice''Alice'
ageundefined30Error on access outside class30 (private)
cityundefined'NY'Error on access outside class, allowed in subclass'NY' (protected)
Key Moments - 3 Insights
Why can't we access the private member 'age' from outside the class?
Because 'age' is marked private, it is only accessible inside the Person class itself, as shown in execution_table row 6 where access outside causes an error.
Can a subclass access protected members of its parent class?
Yes, protected members like 'city' can be accessed inside subclasses, as shown in execution_table row 9 where Employee accesses this.city successfully.
Is a public member accessible from anywhere?
Yes, public members like 'name' can be accessed from any code, demonstrated in execution_table row 5 where p.name is accessed without error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 6. What happens when we try to access p.age outside the class?
AIt causes an error because 'age' is private
BIt returns undefined
CIt returns the value 30
DIt returns null
💡 Hint
Check execution_table row 6 where accessing p.age outside the class causes an error
According to variable_tracker, what is the value of 'city' after instance creation?
AError on access
B'NY'
Cundefined
Dnull
💡 Hint
Look at variable_tracker row for 'city' under 'After Instance Creation'
If we change 'city' from protected to private, what will happen when Employee subclass tries to access it?
AAccess will be allowed
BIt will become public
CAccess will cause an error
DIt will be undefined
💡 Hint
Refer to execution_table rows 9 and 10 about protected vs private access in subclasses
Concept Snapshot
Access modifiers control visibility of class members:
- public: accessible anywhere
- private: accessible only inside the class
- protected: accessible inside class and subclasses
Use them to protect data and control access in TypeScript classes.
Full Transcript
This visual execution trace shows how TypeScript access modifiers public, private, and protected work in a class. We define a class Person with three members: name (public), age (private), and city (protected). Public members can be accessed anywhere, private only inside the class, and protected inside the class and subclasses. We create an instance and try accessing these members from outside and inside a subclass. The trace shows errors when accessing private or protected members outside allowed scopes, and successful access when allowed. This helps beginners understand how access modifiers control visibility and protect data in classes.