Access modifiers are keywords used to control the visibility and accessibility of properties and methods within a class.
These modifiers allow you to define which parts of a class can be accessed from outside the class and which should remain hidden, facilitating encapsulation and safer code design.
The three main access modifiers available in TypeScript are public, private, and protected.
| Modifier | Description |
|---|---|
public | Allows access to properties and methods from any part of the code. It is the default modifier if none is specified. |
private | Restricts access to properties and methods to the class itself, hiding them from derived classes and the rest of the code. |
protected | Allows access to properties and methods within the class itself and its derived classes, but not from outside them. |
Public Modifier
The public modifier is the default in TypeScript and allows properties and methods to be accessible from any part of the code.
This means they can be read and modified from outside the class in which they are defined.
class Student {
public name: string;
public age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public showInfo(): void {
console.log(`Name: ${this.name}, Age: ${this.age}`);
}
}
const student = new Student("Carlos", 21);
console.log(student.name); // Public access
student.showInfo(); // Public method
In this example, both the properties name and age, as well as the method showInfo, are public, so they can be accessed and used outside the Student class.
Private Modifier
The private modifier hides a class’s properties and methods, so that they can only be accessed and modified from within the class itself.
private members are not accessible from outside the class nor from its derived classes.
class BankAccount {
private balance: number;
constructor(initialBalance: number) {
this.balance = initialBalance;
}
public showBalance(): void {
console.log(`Balance: ${this.balance}`);
}
public deposit(amount: number): void {
if (amount > 0) {
this.balance += amount;
}
}
}
const account = new BankAccount(1000);
account.showBalance(); // Output: Balance: 1000
account.deposit(500);
account.showBalance(); // Output: Balance: 1500
console.log(account.balance); // Error: Property 'balance' is private and only accessible within class 'BankAccount'.
Here, the property balance is private, meaning it cannot be accessed directly from outside the BankAccount class. Only the class’s methods, such as deposit and showBalance, can access and modify balance.
Protected Modifier
The protected modifier allows properties and methods to be accessible within the class where they are defined and in its derived classes, but not from outside these classes.
This is useful for allowing derived classes to access members that should not be public.
class Vehicle {
protected brand: string;
constructor(brand: string) {
this.brand = brand;
}
}
class Car extends Vehicle {
public model: string;
constructor(brand: string, model: string) {
super(brand);
this.model = model;
}
// the Car class can use .brand
public showDetails(): void {
console.log(`Brand: ${this.brand}, Model: ${this.model}`);
}
}
const car = new Car("Toyota", "Corolla");
car.showDetails(); // Output: Brand: Toyota, Model: Corolla
// the program cannot use brand directly
console.log(car.brand); // Error: Property 'brand' is protected and only accessible within class 'Vehicle' and its subclasses.
In this example, the property brand is protected, so it can only be accessed within the Vehicle class and its derived classes, like Car. It is not accessible from outside these classes.
