0Pricing
TypeScript Academy · Lesson

Advanced Class Features

Discover advanced class features like abstract classes, access modifiers, and static members to write clean, modular, and object-oriented TypeScript code.

Advanced Class Features is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Advanced Class Features

TypeScript offers several advanced class features that help you write modular, scalable, and maintainable object-oriented code.

In this lesson, you'll learn about access modifiers, abstract classes, static members, and more.

Advanced Class Features — illustration 1

1

Introduction to Advanced Class Features

TypeScript offers several advanced class features that help you write modular, scalable, and maintainable object-oriented code.

In this lesson, you'll learn about access modifiers, abstract classes, static members, and more.

Advanced Class Features — illustration 2

2

Access Modifiers

TypeScript provides three access modifiers to control the visibility of class members:

  • public: Accessible anywhere (default).
  • private: Accessible only within the class.
  • protected: Accessible within the class and its subclasses.
class Person {
  public name: string;
  private age: number;
  protected role: string;

  constructor(name: string, age: number, role: string) {
    this.name = name;
    this.age = age;
    this.role = role;
  }
}

class Employee extends Person {
  getRole(): string {
    return this.role; // Accessible because it's protected
  }
}

3

Abstract Classes

Abstract classes are used as blueprints for other classes. They cannot be instantiated directly and may include abstract methods.

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

class Circle extends Shape {
  constructor(public radius: number) {
    super();
  }

  getArea(): number {
    return Math.PI * this.radius ** 2;
  }
}

const circle = new Circle(5);
console.log(circle.getArea()); // 78.54

4

Static Members

Static members belong to the class itself rather than any instance of the class. They are accessed using the class name.

class MathUtils {
  static PI = 3.14;

  static calculateCircleArea(radius: number): number {
    return this.PI * radius ** 2;
  }
}

console.log(MathUtils.PI); // 3.14
console.log(MathUtils.calculateCircleArea(5)); // 78.5

5

Getters and Setters

Getters and setters allow you to control how properties are accessed or modified in a class.

class Person {
  private _name: string;

  constructor(name: string) {
    this._name = name;
  }

  get name(): string {
    return this._name;
  }

  set name(newName: string) {
    this._name = newName;
  }
}

const person = new Person('Alice');
console.log(person.name); // Alice
person.name = 'Bob';
console.log(person.name); // Bob

6

Readonly Properties

The readonly modifier ensures that a property can only be set during initialization and not modified later.

class Vehicle {
  readonly make: string;

  constructor(make: string) {
    this.make = make;
  }
}

const car = new Vehicle('Toyota');
console.log(car.make); // Toyota
// car.make = 'Honda'; // Error: Cannot assign to 'make' because it is a read-only property.

7

Private and Protected Constructors

Classes can have private or protected constructors to control how and where they are instantiated.

class Singleton {
  private static instance: Singleton;

  private constructor() {}

  static getInstance(): Singleton {
    if (!this.instance) {
      this.instance = new Singleton();
    }
    return this.instance;
  }
}

const singleton = Singleton.getInstance();
console.log(singleton);

8

9

Practice Task

Create a class with a protected constructor and extend it in a subclass. Add methods to access the protected properties in the subclass.

10

Congratulations!

You’ve learned advanced class features like access modifiers, abstract classes, static members, and more. Practice these concepts to write clean and efficient object-oriented TypeScript code!

Advanced Class Features — illustration 11

Frequently asked questions

Is the “Advanced Class Features” lesson free?

Yes — the full text of “Advanced Class Features” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Advanced Class Features”?

Discover advanced class features like abstract classes, access modifiers, and static members to write clean, modular, and object-oriented TypeScript code. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Advanced Class Features” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Advanced Type Narrowing
  2. Advanced Class Features
  3. Conditional Types
← Back to TypeScript Academy