LibraryAccess modifiers: public, private, protected

Access modifiers: public, private, protected

Learn about Access modifiers: public, private, protected as part of TypeScript Full-Stack Development

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

code
public
,
code
private
, and
code
protected
.

Public Access Modifier

Members declared with the

code
public
modifier are accessible from anywhere, both inside and outside the class. This is the default access level in TypeScript if no modifier is specified. It means that a public property or method can be accessed and modified by any part of your program.

What is the default access level for class members in TypeScript if no modifier is specified?

Public

Private Access Modifier

Members declared with the

code
private
modifier are only accessible from within the class itself. They cannot be accessed or modified from outside the class, nor from any derived classes. This is a powerful tool for data hiding and ensuring that the internal state of an object is managed exclusively by its own methods.

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

code
protected
modifier are accessible only within the class itself and by any classes that inherit from it (subclasses or derived classes). They are not accessible from outside the class hierarchy. This modifier is useful when you want to allow subclasses to access and extend certain functionalities while still preventing external access.

ModifierAccessible From Within ClassAccessible From Derived ClassesAccessible From Outside Class
publicYesYesYes
privateYesNoNo
protectedYesYesNo

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

code
public
,
code
private
, or
code
protected
in their declaration, their members are implicitly public. You can access enum members from anywhere once the enum is defined. However, the context in which you use an enum (e.g., within a class property) will be governed by the access modifiers applied to that property.

Are enum members in TypeScript public by default?

Yes

Learning Resources

TypeScript Access Modifiers - Official Documentation(documentation)

The official TypeScript documentation provides a clear and concise explanation of access modifiers, including examples.

Understanding TypeScript Classes and Access Modifiers(blog)

A practical guide that breaks down TypeScript classes and the role of access modifiers with illustrative code snippets.

TypeScript OOP: Encapsulation, Abstraction, Inheritance, Polymorphism(blog)

This article covers core OOP principles in TypeScript, with a dedicated section on access modifiers and their importance for encapsulation.

TypeScript Tutorial: Classes(tutorial)

A comprehensive tutorial on TypeScript classes, which includes detailed explanations and examples of access modifiers.

TypeScript Access Modifiers Explained(video)

A visual explanation of TypeScript access modifiers, demonstrating their usage and impact on code accessibility.

TypeScript Enums - GeeksforGeeks(blog)

Learn about TypeScript enums, their syntax, and how they are used, including their implicit public accessibility.

Mastering TypeScript: Classes and Interfaces(video)

A professional development course that delves deep into TypeScript classes, interfaces, and access modifiers for building robust applications.

TypeScript Access Modifiers - Tutorial Teacher(tutorial)

This resource provides clear examples and explanations of public, private, and protected access modifiers in TypeScript.

Object-Oriented Programming in TypeScript(blog)

An overview of OOP concepts in TypeScript, highlighting how access modifiers contribute to encapsulation and code organization.

TypeScript Handbook - Classes(documentation)

The full chapter on classes in the TypeScript handbook, offering a complete understanding of class features including access modifiers.