Private, Public & Protected Access Modifiers in TypeScript

Private, Public & Protected are the Access Modifiers that we can use in Typescript to control whether certain methods or properties are visible to code outside the class. In this tutorial, let us learn about the similarities and differences between them.

Public

Public access modifier allows the class properties and method freely accessible anywhere in the code. The public is the default access modifier if we do not specify the access modifier

In this example, the Person class has two properties firstName & lastName. It also contains a method getName. All of them are prefixed with the keyword public. As you can see we can access all of these properties outside the class.

The above code works even if we remove the keyword public. Because it is the default access modifier.

Private

The Private access modifier restricts access to the class member from within the class only. We cannot access the property or the method from outside the class

In this example, we mark both firstName & lastName as Private. When we try to access them outside the class we get the compiler error Property 'firstName' is private and only accessible within class.

We cannot even access the private property in a derived class. In this example, the Employee class extends the Person class. The changeName method of the Employee class tries to access the firstName & lastName of the Person class. The compiler throws the Property is private and only accessible within class 'Person' error.

Protected

The Protected modifier allows access to the class member from itself and from any classes that inherit (sub-class) from it.

In this example, we have marked the firstName & lastName as Protected. We are able to modify these properties in the changeName method of the derived class Employee.

But accessing these members in the instance created using the Person class or using the Employee class results in a compiler error.

Overriding Access Modifier in the Derived Class

The derived class can redeclare the property and override the Access modifier. In the example, the derived class Employee re-declares the protected property firstName & lastName. This effectively removes the protected protection from the objects created using the Employee class. But the objects created using the Person class will not be able to access the firstName & lastName

Readonly Vs Public, Private & Protected

The ReadOnly keyword makes the property read-only. That is you can only initialize it either when you declare it or inside the constructor function. Assigning any value to the readonly property anywhere else results in an error. But it does not prevent you from accessing it. It only prevents you from assigning a value.

Private & Protected prevents you from accessing the variable itself.

We can mix and match both. In this example we mark firstName & lastName as both protected and read-only. We can access the property in the changeName method the derived class Employee. But we cannot modify it because it is protected.

Optional Property

You can also make the property Optional along with the access modifier and readonly keyword.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top