In TypeScript, access modifiers control the visibility of class members, such as properties and methods. They define which parts of the code can access these members.
Public:
The default modifier in TypeScript.
Allows members to be accessed by any class, inside or outside the class in which they were declared.
Example:
class Person {
public name: string;
constructor(name: string) {
this.name = name;
}
public greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
const person = new Person('John Doe');
person.greet(); // Output: Hello, my name is John Doe
Private:
Restricts access to members to the class in which they are declared.
Example:
class Person {
private name: string;
constructor(name: string) {
this.name = name;
}
public greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
const person = new Person('John Doe');
// person.name is inaccessible outside the Person class
Protected:
Allows access to members within the class in which they are declared and its subclasses.
Example:
class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
protected greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
class Student extends Person {
public studentId: number;
constructor(name: string, studentId: number) {
super(name);
this.studentId = studentId;
}
public override greet() {
super.greet();
console.log(`I am also a student with ID ${this.studentId}`);
}
}
const student = new Student('John Doe', 12345);
student.greet(); // Output: Hello, my name is John Doe. I am also a student with ID 12345
Abstract:
Indicates that a method or property must be implemented in derived classes.
Abstract members cannot have a direct implementation in the base class.
Example:
abstract class Shape {
abstract getArea(): number;
public abstract toString(): string;
}
class Circle extends Shape {
private radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
public getArea(): number {
return Math.PI * this.radius * this.radius;
}
public toString(): string {
return `Circle with radius ${this.radius}`;
}
}
const circle = new Circle(5);
console.log(circle.getArea()); // Output: 78.53975
console.log(circle.toString()); // Output: Circle with radius 5
In summary, access modifiers control the accessibility of class members, while the abstract modifier defines incomplete methods or properties that must be implemented in subclasses.
Public: The default modifier, allowing members to be accessed anywhere.
Private: Restricts access to the class where the member is declared.
Protected: Allows access within the class and its subclasses.
Abstract: Requires derived classes to implement incomplete methods or properties.
Modified | Visibility | Details |
---|---|---|
public | Accessible anywhere | Pattern for class members |
private | Only within the class | Greater encapsulation |
protected | inside the class and derived classes | Promotes reuse in inheritance |
abstract | No implementation | Defines a contract for derived classes |
Tip:
Remember to choose the appropriate access modifier for each class member, considering the desired level of encapsulation and reuse.
Sources :
github.com/Mcps4/ifpi-ads-eng3-2022
Top comments (0)