Abstract Classes

Chapter: Classes and Objects / Section: Advanced Class Features

Abstract Classes

A comprehensive guide to Abstract Classes in Typescript. Learn about creating and using abstract classes with clear explanations. Perfect for beginners starting with Typescript.

Introduction

Abstract classes are a powerful feature in Typescript that allow you to define a base class with abstract methods that must be implemented by derived classes. They provide a way to enforce a certain structure and behavior in your class hierarchies, making your code more modular and maintainable. Understanding abstract classes is crucial for creating robust and scalable Typescript applications.

In this article, we'll dive deep into abstract classes in Typescript. You'll learn the core concepts, how to implement them, best practices, common pitfalls to avoid, and see practical examples. By the end, you'll have a solid grasp of abstract classes and be able to use them effectively in your own projects.

Core Concepts

An abstract class is a special type of class that cannot be instantiated directly. It serves as a base class for other classes to inherit from and provides a common interface and implementation. Here are the key characteristics of abstract classes:

  • Declared using the abstract keyword
  • Can contain abstract methods that derived classes must implement
  • May also include non-abstract methods with default implementation
  • Cannot be directly instantiated using the new keyword

Abstract methods are declared without an implementation and must be overridden by derived classes. They are defined using the abstract keyword followed by the method signature.

abstract class Animal { abstract makeSound(): void; }

Implementation Details

To create an abstract class, use the abstract keyword before the class declaration. Abstract methods are defined within the class using the abstract keyword and do not have an implementation.

abstract class Shape { abstract calculateArea(): number; }

To create a derived class that inherits from an abstract class, use the extends keyword and provide implementations for all abstract methods.

class Circle extends Shape { constructor(private radius: number) { super(); } calculateArea(): number { return Math.PI * this.radius ** 2; } }

Best Practices

  • Use abstract classes to define a common interface and behavior for related classes.
  • Keep abstract classes focused and cohesive, representing a single concept or responsibility.
  • Provide default implementations for non-abstract methods when appropriate.
  • Use access modifiers (public, protected, private) to control visibility and encapsulation.

Common Pitfalls

  • Avoid creating deep inheritance hierarchies with abstract classes, as it can lead to complexity and tight coupling.
  • Be cautious when adding non-abstract methods to abstract classes, ensuring they are relevant and applicable to all derived classes.

Practical Examples

abstract class Employee { constructor(private name: string) {} abstract calculateSalary(): number; getEmployeeDetails(): string { return `Name: ${this.name}, Salary: ${this.calculateSalary()}`; } } class FullTimeEmployee extends Employee { constructor(name: string, private salary: number) { super(name); } calculateSalary(): number { return this.salary; } } class ContractEmployee extends Employee { constructor(name: string, private hourlyRate: number, private hoursWorked: number) { super(name); } calculateSalary(): number { return this.hourlyRate * this.hoursWorked; } }

Summary and Next Steps

In this article, we explored abstract classes in Typescript. We covered the core concepts, implementation details, best practices, and common pitfalls. We also saw practical examples of how to create and use abstract classes.

Abstract classes provide a powerful way to define common behavior and enforce a certain structure in your class hierarchies. They promote code reuse, modularity, and maintainability.

To further enhance your understanding of abstract classes, consider exploring related topics such as interfaces, inheritance, and polymorphism. Practice using abstract classes in your own projects to solidify your knowledge and gain hands-on experience.