In Typescript, Access Modifiers are keywords used to control the visibility of class members (properties and methods).
Access Modifiers help in achieving encapsulation, which is a fundamental principle of object-oriented programming (OOP).
Access modifiers ensure that the properties of an object are not directly accessible from outside the class, promoting better code organization, maintainability, and reusability.
There are three main access modifiers:
public
private
protected
These modifiers determine where a class member can be accessed.
The public modifier allows a class member to be accessed from anywhere, both within the class and from external code.
class PublicModifierExample { public publicProperty: string; constructor(publicProperty: string) { this.publicProperty = publicProperty; } public publicMethod(): void { console.log("This method is public method."); } } let publicModifierInstance = new PublicModifierExample("Public Modifier Example"); // Accessing public property console.log(publicModifierInstance.publicProperty); // Output: "Public Modifier Example" // Calling public method publicModifierInstance.publicMethod(); // Output: "This method is public method."
The private modifier restricts access to the class members only within the same class.
class PrivateModifierExample { private privateProperty: string; constructor(privateProperty: string) { this.privateProperty = privateProperty; } private privateMethod(): void { console.log("This method is private."); } public accessPrivateMethod(): void { this.privateMethod(); // Accessing private method within the class } } let privateModifierInstance = new PrivateModifierExample("Private Modifier Example"); console.log(privateModifierInstance.privateProperty); // Error: Property 'privateProperty' is private and only accessible within class 'Example'. privateModifierInstance.privateMethod(); // Error: Property 'privateMethod' is private and only accessible within class 'Example'. privateModifierInstance.accessPrivateMethod(); // Accessing private method via a public method
They cannot be accessed from outside the class.
class Parent { protected protectedProperty: string; constructor(protectedProperty: string) { this.protectedProperty = protectedProperty; } protected protectedMethod(): void { console.log("This method is protected method."); } } class Child extends Parent { public accessProtected(): void { console.log(this.protectedProperty); // Accessing protected property from a derived class this.protectedMethod(); // Accessing protected method from a derived class } } let childInstance = new Child("Protected Modifier Example"); console.log(childInstance.protectedProperty); // Error: Property 'protectedProperty' is protected and only accessible within class 'Parent' and its subclasses. childInstance.protectedMethod(); // Error: Property 'protectedMethod' is protected and only accessible within class 'Parent' and its subclasses. childInstance.accessProtected(); // Accessing protected property and method through a derived class
The protected modifier is similar to private, but it also allows access within derived classes (subclasses).
public: No restrictions on access.
private: Accessible only within the declaring class.
protected: Accessible within the declaring class and its subclasses.