Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

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

bookProtected Members

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt