Understanding Access Modifiers in TypeScript
In object-oriented programming, access modifiers are keywords used to control the visibility and accessibility of class members (properties and methods). TypeScript, a superset of JavaScript, supports several access modifiers to enforce encapsulation and structure your code effectively. This module will explore
public
private
protected
Public Access Modifier
Members declared with the
public
Public
Private Access Modifier
Members declared with the
private
Think of private
members like the internal workings of a complex machine. Only the machine's own engineers (the class methods) can directly interact with and adjust those parts.
Protected Access Modifier
Members declared with the
protected
Modifier | Accessible From Within Class | Accessible From Derived Classes | Accessible From Outside Class |
---|---|---|---|
public | Yes | Yes | Yes |
private | Yes | No | No |
protected | Yes | Yes | No |
Practical Application: Encapsulation
Access modifiers are fundamental to the principle of encapsulation, a core concept in object-oriented programming. Encapsulation bundles data (properties) and methods that operate on the data within a single unit (a class), and restricts direct access to some of the object's components. This helps in managing complexity, improving code maintainability, and preventing unintended side effects.
Consider a BankAccount
class. The balance
should ideally be private
to prevent direct manipulation. We can provide public
methods like deposit()
and withdraw()
to manage the balance. If we have a SavingsAccount
that inherits from BankAccount
, we might make certain internal calculation methods protected
so the SavingsAccount
can use them, but external code cannot.
Text-based content
Library pages focus on text content
Enums and Access Modifiers
Enums in TypeScript are a way to define a set of named constants. While enums themselves don't directly use access modifiers like
public
private
protected
Yes
Learning Resources
The official TypeScript documentation provides a clear and concise explanation of access modifiers, including examples.
A practical guide that breaks down TypeScript classes and the role of access modifiers with illustrative code snippets.
This article covers core OOP principles in TypeScript, with a dedicated section on access modifiers and their importance for encapsulation.
A comprehensive tutorial on TypeScript classes, which includes detailed explanations and examples of access modifiers.
A visual explanation of TypeScript access modifiers, demonstrating their usage and impact on code accessibility.
Learn about TypeScript enums, their syntax, and how they are used, including their implicit public accessibility.
A professional development course that delves deep into TypeScript classes, interfaces, and access modifiers for building robust applications.
This resource provides clear examples and explanations of public, private, and protected access modifiers in TypeScript.
An overview of OOP concepts in TypeScript, highlighting how access modifiers contribute to encapsulation and code organization.
The full chapter on classes in the TypeScript handbook, offering a complete understanding of class features including access modifiers.