ReasonJun

Typescript : Class & Access Modifiers 본문

Frontend/Typescript

Typescript : Class & Access Modifiers

ReasonJun 2023. 6. 10. 13:20
728x90

In TypeScript, access modifiers are keywords that determine the accessibility of class members (properties and methods) from outside the class. TypeScript provides three access modifiers: public, private, and protected. These access modifiers control how class members can be accessed and modified.

 

Here's an overview of each access modifier:

  • public: The public access modifier allows class members to be accessed from anywhere, both within the class and from outside the class.
class MyClass {
  public publicProperty: string;

  public publicMethod(): void {
    // Code here
  }
}

const obj = new MyClass();
obj.publicProperty = 'Hello';
obj.publicMethod();

In the example above, the publicProperty and publicMethod are accessible from outside the MyClass class, so you can access and modify them using an instance of the class.

  • private: The private access modifier restricts access to class members only within the class where they are defined. They cannot be accessed or modified from outside the class.
class MyClass {
  private privateProperty: string;

  private privateMethod(): void {
    // Code here
  }
}

const obj = new MyClass();
obj.privateProperty; // Error: Property 'privateProperty' is private
obj.privateMethod(); // Error: Property 'privateMethod' is private

In this example, the privateProperty and privateMethod are private, so attempting to access or modify them from outside the class results in a compilation error.

  • protected: The protected access modifier allows class members to be accessed within the class where they are defined and by subclasses that inherit from the class.
class MyBaseClass {
  protected protectedProperty: string;

  protected protectedMethod(): void {
    // Code here
  }
}

class MyDerivedClass extends MyBaseClass {
  public useProtectedMember(): void {
    this.protectedProperty = 'Hello';
    this.protectedMethod();
  }
}

const obj = new MyBaseClass();
obj.protectedProperty; // Error: Property 'protectedProperty' is protected
obj.protectedMethod(); // Error: Property 'protectedMethod' is protected

const derivedObj = new MyDerivedClass();
derivedObj.useProtectedMember(); // Can access protected members

In this example, the protectedProperty and protectedMethod are accessible within the MyBaseClass, but not from outside the class. However, they can be accessed by a subclass like MyDerivedClass, and instances of the subclass can use these protected members.

 

class

To use access modifiers in a class, you can apply them to class members such as properties and methods. Here's an example of how access modifiers can be used in a class:

class MyClass {
  public publicProperty: string;
  private privateProperty: number;
  protected protectedProperty: boolean;

  constructor(publicProperty: string, privateProperty: number, protectedProperty: boolean) {
    this.publicProperty = publicProperty;
    this.privateProperty = privateProperty;
    this.protectedProperty = protectedProperty;
  }

  public publicMethod(): void {
    console.log('This is a public method.');
  }

  private privateMethod(): void {
    console.log('This is a private method.');
  }

  protected protectedMethod(): void {
    console.log('This is a protected method.');
  }
}

When creating an instance of the MyClass, you can interact with the public members and call the public methods:

const myObj = new MyClass('Public', 42, true);
console.log(myObj.publicProperty); // Output: Public
myObj.publicMethod(); // Output: This is a public method.

However, attempting to access private or protected members from outside the class will result in a compilation error:

console.log(myObj.privateProperty); // Error: Property 'privateProperty' is private.
myObj.privateMethod(); // Error: Property 'privateMethod' is private.
console.log(myObj.protectedProperty); // Error: Property 'protectedProperty' is protected.
myObj.protectedMethod(); // Error: Property 'protectedMethod' is protected.

 

Access modifiers provide control over the encapsulation and visibility of class members, allowing you to define the desired level of accessibility and protect sensitive data. By using access modifiers effectively, you can enforce encapsulation, prevent unauthorized access, and design more secure and maintainable code.

728x90
Comments