Tutorials Logic, IN info@tutorialslogic.com

TypeScript Classes: Access Modifiers, Constructors and Implements

Class Basics

TypeScript classes add type checking to JavaScript class syntax. They help you model objects that combine data and behavior, such as users, invoices, carts, repositories, services, UI components, and domain entities.

A TypeScript class can use constructors, public/private/protected members, readonly properties, getters, setters, static members, abstract classes, inheritance, and `implements` clauses. The goal is not to put everything into classes; the goal is to use classes when encapsulated state and behavior make the code clearer.

A class is a blueprint for objects. The constructor initializes each instance, methods define behavior, and properties store instance data. TypeScript checks that values assigned to properties match their declared types.

  • `public` members can be used anywhere and are the default.
  • `private` members can only be used inside the class.
  • `protected` members can be used inside the class and subclasses.
  • `readonly` properties can be assigned during initialization but not changed later.
  • Parameter properties let you declare and assign constructor properties in one line.

Class With Access Modifiers

Class With Access Modifiers
class Course {
  private enrolled = 0;

  constructor(
    public readonly id: number,
    public title: string,
    private maxStudents: number
  ) {}

  enroll(): void {
    if (this.enrolled >= this.maxStudents) {
      throw new Error("Course is full");
    }
    this.enrolled++;
  }

  get seatsLeft(): number {
    return this.maxStudents - this.enrolled;
  }
}

const course = new Course(1, "TypeScript Classes", 30);
course.enroll();
console.log(course.title, course.seatsLeft);

Implements and Interfaces

The `implements` keyword checks that a class follows an interface shape. It does not copy code from the interface; it only verifies that the required members exist with compatible types.

  • Use interfaces to describe required behavior.
  • Use classes to provide implementation and state.
  • A class can implement multiple interfaces.
  • An interface cannot enforce private implementation details.

Class Implementing an Interface

Class Implementing an Interface
interface NotificationSender {
  send(to: string, message: string): Promise<void>;
}

class EmailSender implements NotificationSender {
  constructor(private fromAddress: string) {}

  async send(to: string, message: string): Promise<void> {
    console.log(`Email from ${this.fromAddress} to ${to}: ${message}`);
  }
}

async function notifyUser(sender: NotificationSender) {
  await sender.send("student@example.com", "Your course is ready.");
}

notifyUser(new EmailSender("noreply@tutorialslogic.com"));

Abstract Classes

An abstract class is useful when related classes share some implementation but must define certain methods themselves. It can hold common properties and methods while leaving specialized behavior to subclasses.

  • You cannot create an instance of an abstract class directly.
  • Abstract methods must be implemented by concrete subclasses.
  • Prefer interfaces when you only need a shape; use abstract classes when shared code is valuable.

Abstract Base Class

Abstract Base Class
abstract class ReportExporter {
  constructor(protected fileName: string) {}

  abstract extension(): string;
  abstract render(): string;

  outputPath(): string {
    return `${this.fileName}.${this.extension()}`;
  }
}

class CsvExporter extends ReportExporter {
  extension(): string {
    return "csv";
  }

  render(): string {
    return "name,total\nAsha,1200";
  }
}

const exporter = new CsvExporter("orders");
console.log(exporter.outputPath());
console.log(exporter.render());
Before you move on

TypeScript Classes: Access Modifiers, Constructors and Implements Mastery Check

5 checks
  • A class is a blueprint for objects.
  • The constructor initializes each instance, methods define behavior, and properties store instance data.
  • TypeScript checks that values assigned to properties match their declared types.
  • The `implements` keyword checks that a class follows an interface shape.
  • It does not copy code from the interface; it only verifies that the required members exist with compatible types.

TypeScript Classes Questions Learners Ask

They use JavaScript class runtime behavior, but TypeScript adds compile-time type checking, access modifiers, parameter properties, and abstract class checks.

Use functions for stateless behavior and classes when you need objects with encapsulated state and related methods.

implements is a compile-time contract.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.