Access Modifiers in TypeScript

Access modifiers are keywords used to control the visibility and accessibility of class members (properties and methods) within a class and from external code. The three main access modifiers are public, private, and protected. The public modifier allows members to be accessed from any location, the private modifier restricts access to within the declaring class, and the protected modifier allows access within the declaring class and its subclasses.

These modifiers enhance encapsulation and help enforce proper usage of class members, contributing to the design of robust and maintainable object-oriented code. They ensure that certain aspects of a class's internal implementation are hidden from external code, reducing the risk of unintended interference and promoting a more controlled and secure system.

Public Modifiers

Marked with public, member variables and methods are accessible anywhere in the code, from within the class to any other part of the program.

class User { public name: string; constructor(name: string) { this.name = name; } public greet(): void { console.log(`Hello, my name is ${this.name}!`); } } const john = new User("John Doe"); john.greet(); // Outputs "Hello, my name is John Doe!"

Private Modifiers

Marked with private, member variables and methods are only accessible within the class where they are defined. Any attempt to access them from outside results in an error.

class BankAccount { private balance: number; constructor(initialBalance: number) { this.balance = initialBalance; } private deposit(amount: number): void { this.balance += amount; } public withdraw(amount: number): boolean { if (amount <= this.balance) { this.balance -= amount; return true; } return false; } public getBalance(): number { return this.balance; // Controlled access through a public method } } const account = new BankAccount(100); // Error: balance is private: account.deposit(50); account.withdraw(75); console.log(account.getBalance()); // Outputs 25

Protected Modifiers

Marked with protected, member variables and methods are accessible within the class and its subclasses, providing controlled inheritance.

class Vehicle { protected speed: number; constructor(initialSpeed: number) { this.speed = initialSpeed; } protected accelerate(amount: number) { this.speed += amount; } } class Car extends Vehicle { public startEngine() { console.log("Engine started!"); } public increaseSpeed(amount: number) { this.accelerate(amount); // Accessing protected member from subclass console.log(`Car is now at speed ${this.speed}.`); } } const car = new Car(0); car.startEngine(); car.increaseSpeed(20); // Error: speed is protected, not publicly accessible

Choosing the Right Modifier

  1. Public: Use for frequently accessed data or functionality needed by external code.
  2. Private: Use for sensitive data or internal implementation details not meant for direct outside access.
  3. Protected: Use for data and methods shared across related classes in an inheritance hierarchy.

Conclusion

Access modifiers (public, private, and protected) control the visibility and accessibility of class members. public allows access from anywhere, private restricts access to the defining class, and protected permits access within the class and its subclasses, contributing to encapsulation and code organization.