What are access modifiers in TypeScript?

An access modifier specifies the accessibility of the class variable or method outside the class. Three access modifiers are available:

  1. Public
  2. Private
  3. Protected

Public access modifier

Public variables and methods can be accessed by other classes and objects. By default, the members of a class are public in TypeScript. To declare a public variable/method, we use the public keyword before the declaration of the variable or method.

class User {
name:string; //public variable
constructor(name:string) {
this.name = name;
}
getName() { //public method
return this.name;
}
toString():string { //public method
return this.name;
}
}
let user = new User("Ram");
console.log("user.name => " + user.name);
console.log("user.getName() => " + user.getName());
console.log("user.toString() => " + user.toString());

In the above code, we create a User class with a public class variable name and two public methods, getname and toString . All the public fields are accessible outside the class using the class objects.

Private access modifier

Private variables and methods cannot be accessed outside the classes. The private members can be accessed only inside the class in which it is created.

class User {
private name:string;
constructor(name:string) {
this.name = name;
}
private getName() { //private method
return this.name; // private member can be accessed inside the class
}
public toString() { // private methods can be accessed inside the class
return this.getName();
}
}
let user = new User("Ram");
console.log("user.toString => " + user.toString());
// we will get compilation error for the below lines
// console.log("user.name => " + user.name);
// console.log("user.getName() => " + user.getName());

In the above code, we create a User class with a private class variable name , one private methods getName, and one public method toString. In the getName method, we access the private variable name. In the toString method, we access the private method getName. This is valid because private members can be accessed inside the class.

Add the commented code in lines 20 and 21. And try to access the private variable and private method. The private variable and method cannot be accessed outside the class. Then, uncomment the code and click the "Run" button to check the compilation error.

Protected access modifier

Protected variables and methods can be accessed inside the containing class and its child classes (derived classes).

class User {
protected name:string;
constructor(name:string) {
this.name = name;
}
protected toString() {
// protected variables can be accessed inside the class
console.log("Name :" + this.name);
}
}
class ChildUser extends User{
constructor(name:string) {
super(name);
}
getProtectedName() {
//protected member/methods of parent class can be accessed by its chiild classes
this.toString();
return this.name;
}
}
let user = new ChildUser("Ram");
console.log("user.toString => " + user.getProtectedName());
// we will get compilation error for the below lines
// because the protected members cannot be accessed outside the containing and sub classes
// console.log("user.name => " + user.name);
// console.log("user.getName() => " + user.toString());

In the above code, we create a User class with a protected class variable name and a method getName . We create a child class ChildUser by extending the User class. In the ChildUser class, we create a method getProtectedName. Inside this method, we access the protected variable name and protected method toString of the parent class.

Add commented code in lines 27 and 28. Then, try to access the protected variable name and method toString . The protected variable and method cannot be accessed outside the containing class and its subclasses. In the end, uncomment the code and click the "Run" button to check the compilation error.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved