Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Protected Members | Access Modifiers in TypeScript
TypeScript Classes and OOP

bookProtected 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.

12345678910111213141516171819202122232425262728
class 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.
copy

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.

question mark

Which access modifier allows a property to be accessed in subclasses but not outside the class hierarchy?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5

bookProtected Members

Svep för att visa menyn

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.

12345678910111213141516171819202122232425262728
class 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.
copy

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.

question mark

Which access modifier allows a property to be accessed in subclasses but not outside the class hierarchy?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt