Encapsulation in TypeScript - Protecting Data


Introduction

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP). It refers to the concept of restricting access to certain parts of an object, typically its internal data or implementation details, while exposing a controlled interface for interacting with the object. In TypeScript, you can achieve encapsulation through access modifiers. In this guide, we'll explore the importance of encapsulation and how to implement it in TypeScript.


Why Encapsulation?

Encapsulation provides several benefits, including:

  • Data Protection: It prevents direct access and modification of an object's internal data, reducing the risk of unintended data corruption.
  • Controlled Access: It allows you to specify which parts of an object are accessible from the outside and which are not, promoting a clear and consistent interface.
  • Flexibility: It enables you to change the internal implementation of a class without affecting external code that relies on its public interface.

Access Modifiers

In TypeScript, you can use access modifiers to control the visibility and accessibility of class members, including properties and methods. There are three main access modifiers:

  • Public (default): Members are accessible from anywhere.
  • Private: Members are only accessible within the defining class.
  • Protected: Members are accessible within the defining class and its subclasses (derived classes).

Implementing Encapsulation

To implement encapsulation, you can use private and protected access modifiers to hide internal data and expose a controlled interface for interacting with the object.


Example:

class BankAccount {
private balance: number;
protected accountNumber: string;
constructor(accountNumber: string) {
this.accountNumber = accountNumber;
this.balance = 0;
}
deposit(amount: number): void {
this.balance += amount;
}
withdraw(amount: number): void {
if (amount <= this.balance) {
this.balance -= amount;
}
}
getBalance(): number {
return this.balance;
}
}
class SavingsAccount extends BankAccount {
private interestRate: number;
constructor(accountNumber: string, interestRate: number) {
super(accountNumber);
this.interestRate = interestRate;
}
calculateInterest(): number {
return this.getBalance() * this.interestRate;
}
}

Benefits of Encapsulation

Encapsulation helps ensure data integrity, improves code maintainability, and provides a clear separation between an object's internal implementation and its external interface. It also supports the principle of information hiding, which is essential for building robust and secure applications.


Conclusion

Encapsulation is a fundamental concept in TypeScript and OOP that allows you to protect and control access to an object's internal data. By using access modifiers, you can create classes with well-defined interfaces and maintainable implementations. Encapsulation is a key element in building reliable and secure software systems.