TypeScript Access Modifiers
Understanding all TypeScript access modifiers
Let's understand the access modifiers by a simple table.
In TypeScript, by default all the members of a class are public. The public data member can be accessed anywhere without any restriction.
In the above example, studCode is declared as public and studName is declared without a modifier, so TypeScript treats them as public by default. Since data members are public, they can be accessible outside of the class using an object of the class.
The private access modifier cannot be accessible outside of its containing class. It ensures that the class members are visible only to that class in which it is containing.
In the above example, studCode is declared as private, and studName is declared without a modifier, so TypeScript treats them as public by default. If we access the private member outside of the class, it will give a compile error.
A Protected access modifier can be accessed only within the class and its subclass. It cannot be accessed outside of the class in which it is containing.
My unique code: 1, my name: JoeRoot and I am in CS Branch. undefined
In the above example, we can't use the name from outside of Student class. We can still use it from within an instance method of Person because Person class derives from Student class.