In TypeScript, a class is a blueprint for creating objects that share similar properties and methods. It is a fundamental concept in Object-Oriented Programming (OOP) and provides a way to encapsulate data and behavior within a single entity.
TypeScript classes support features like inheritance, interfaces, access modifiers, method overloading, and static properties. These help in creating more structured and reusable code.
Let’s break down the main features of TypeScript classes and how they support OOP principles.
1. Encapsulation:
Encapsulation is the concept of bundling data (properties) and operations (methods) within a single unit, the class. This makes it possible to restrict access to certain properties and methods, ensuring a consistent state for the object.
In TypeScript, you can create a class using the ‘class‘ keyword, followed by the class name:
class Person {
private name: string;
private age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public sayHello(): string {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
2. Inheritance:
Inheritance allows a class (the derived class) to inherit properties and methods from another class (the base class). This promotes the principle of reusability in OOP.
In TypeScript, you can use the ‘extends‘ keyword for inheritance:
class Employee extends Person {
private role: string;
constructor(name: string, age: number, role: string) {
super(name, age); // call the base class's constructor
this.role = role;
}
public getRole(): string {
return this.role;
}
}
3. Abstraction and Interfaces:
Abstraction is the process of defining a contract for a class while leaving the implementation details to the derived classes. In TypeScript, you can achieve this using interfaces or abstract classes.
Interfaces define a contract that a class must implement, and TypeScript enforces this contract during compilation:
interface IPayroll {
calculateSalary(): number;
}
class FullTimeEmployee extends Employee implements IPayroll {
private salary: number;
constructor(name: string, age: number, role: string, salary: number) {
super(name, age, role);
this.salary = salary;
}
public calculateSalary(): number {
return this.salary;
}
}
Abstract classes allow you to create methods or properties that must be implemented or overridden in a derived class:
abstract class Animal {
abstract makeSound(): string;
}
class Dog extends Animal {
public makeSound(): string {
return 'Woof!';
}
}
4. Polymorphism:
Polymorphism enables multiple implementations of a method with the same name in different classes or in derived classes. In TypeScript, you can achieve polymorphism with method overriding and interfaces:
class PartTimeEmployee extends Employee implements IPayroll {
private hourlyRate: number;
private hoursWorked: number;
constructor(name: string, age: number, role: string, hourlyRate: number, hoursWorked: number) {
super(name, age, role);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
public calculateSalary(): number {
return this.hourlyRate * this.hoursWorked;
}
}
In the example above, both ‘FullTimeEmployee‘ and ‘PartTimeEmployee‘ implement the ‘IPayroll‘ interface and provide their own implementation for the ‘calculateSalary‘ method.
In summary, TypeScript classes provide a powerful way to model real-world objects using OOP principles like encapsulation, inheritance, abstraction, and polymorphism. They help in organizing code, making it more modular, reusable, and maintainable.