Protected Members
In TypeScript, the protected access modifier allows you to control the visibility of properties and methods within a class hierarchy. When you declare a member as protected, it is accessible inside the class where it is defined, as well as in any subclasses that extend it. However, it cannot be accessed from outside the class hierarchy, such as from instances or unrelated classes. This makes protected especially useful for cases where you want to share functionality or data with subclasses, but you do not want those members to become part of the public API.
12345678910111213141516171819202122232425262728class Employee { protected name: string; constructor(name: string) { this.name = name; } protected getDetails(): string { return `Employee: ${this.name}`; } } class Manager extends Employee { private department: string; constructor(name: string, department: string) { super(name); this.department = department; } public describe(): string { // Accessing protected member 'name' and method 'getDetails' from the base class return `${this.getDetails()}, Department: ${this.department}`; } } const mgr = new Manager("Alice", "Engineering"); console.log(mgr.describe()); // Employee: Alice, Department: Engineering // mgr.name; // Error: Property 'name' is protected and only accessible within class 'Employee' and its subclasses.
You should use protected when you want to allow subclasses to access or modify a member, but you do not want it to be accessible from outside the class hierarchy. In contrast, private members are only accessible within the class where they are declared and cannot be accessed by subclasses. Public members, on the other hand, can be accessed from anywhere. Choosing between these access modifiers depends on how much visibility and control you want to provide for your class members, especially when designing for inheritance and extensibility.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Can you explain the difference between `protected` and `private` in more detail?
When should I use `protected` instead of `public` or `private`?
Can you give more examples of using `protected` in TypeScript?
Awesome!
Completion rate improved to 5
Protected Members
Свайпніть щоб показати меню
In TypeScript, the protected access modifier allows you to control the visibility of properties and methods within a class hierarchy. When you declare a member as protected, it is accessible inside the class where it is defined, as well as in any subclasses that extend it. However, it cannot be accessed from outside the class hierarchy, such as from instances or unrelated classes. This makes protected especially useful for cases where you want to share functionality or data with subclasses, but you do not want those members to become part of the public API.
12345678910111213141516171819202122232425262728class Employee { protected name: string; constructor(name: string) { this.name = name; } protected getDetails(): string { return `Employee: ${this.name}`; } } class Manager extends Employee { private department: string; constructor(name: string, department: string) { super(name); this.department = department; } public describe(): string { // Accessing protected member 'name' and method 'getDetails' from the base class return `${this.getDetails()}, Department: ${this.department}`; } } const mgr = new Manager("Alice", "Engineering"); console.log(mgr.describe()); // Employee: Alice, Department: Engineering // mgr.name; // Error: Property 'name' is protected and only accessible within class 'Employee' and its subclasses.
You should use protected when you want to allow subclasses to access or modify a member, but you do not want it to be accessible from outside the class hierarchy. In contrast, private members are only accessible within the class where they are declared and cannot be accessed by subclasses. Public members, on the other hand, can be accessed from anywhere. Choosing between these access modifiers depends on how much visibility and control you want to provide for your class members, especially when designing for inheritance and extensibility.
Дякуємо за ваш відгук!