⊗tsSpOpACl 17 of 37 menu

Abstract OOP classes in TypeScript

There are situations when we have a parent class that contains common properties and methods, and child classes inherit from it.

It may be that we will create objects of descendant classes, but not of the parent class. We only need it to group common properties and methods.

In this case, to explicitly prohibit the creation of objects of the parent class, you can declare it abstract. This is done using the abstract keyword.

Let's look at an example. Let's take our class User and declare it abstract:

abstract class User { public name: string; constructor(name: string) { this.name = name; } }

Let's make a class Student that inherits from User:

class Student extends User { public course: number; constructor(name: string, course: number) { super(name); this.course = course; } }

Let's make a class Employee that inherits from User:

class Employee extends User { public salary: number; constructor(name: string, salary: number) { super(name); this.salary = salary; } }

Create an abstract class Figure that represents a geometric figure. Let it have properties for perimeter and area.

Make class Square inherit from class Figure.

Make class Rectangle inherit from class Figure.

csituzcdauzl