Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Implementing Abstract Classes | Abstract Classes and Interfaces
TypeScript Classes and OOP

bookImplementing Abstract Classes

123456789101112131415161718192021222324252627
abstract class Shape { abstract getArea(): number; abstract getPerimeter(): number; } class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } getArea(): number { return this.width * this.height; } getPerimeter(): number { return 2 * (this.width + this.height); } } const rect = new Rectangle(10, 5); console.log(rect.getArea()); // 50 console.log(rect.getPerimeter()); // 30
copy

When you use an abstract class in TypeScript, you define methods that have no implementation, called abstract methods. These methods act as a contract: any subclass that extends the abstract class must provide concrete implementations for all abstract methods. This ensures that every subclass fulfills the required behaviors specified by the abstract class, helping you enforce consistent APIs and reducing errors from missing functionality. If a subclass does not implement all abstract methods, TypeScript will report a compilation error, preventing incomplete implementations.

question mark

What must a subclass do when it extends an abstract class with abstract methods?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the difference between abstract classes and interfaces in TypeScript?

Can you show an example of another shape class that extends the abstract Shape class?

What happens if I try to instantiate the abstract Shape class directly?

Awesome!

Completion rate improved to 5

bookImplementing Abstract Classes

Свайпніть щоб показати меню

123456789101112131415161718192021222324252627
abstract class Shape { abstract getArea(): number; abstract getPerimeter(): number; } class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } getArea(): number { return this.width * this.height; } getPerimeter(): number { return 2 * (this.width + this.height); } } const rect = new Rectangle(10, 5); console.log(rect.getArea()); // 50 console.log(rect.getPerimeter()); // 30
copy

When you use an abstract class in TypeScript, you define methods that have no implementation, called abstract methods. These methods act as a contract: any subclass that extends the abstract class must provide concrete implementations for all abstract methods. This ensures that every subclass fulfills the required behaviors specified by the abstract class, helping you enforce consistent APIs and reducing errors from missing functionality. If a subclass does not implement all abstract methods, TypeScript will report a compilation error, preventing incomplete implementations.

question mark

What must a subclass do when it extends an abstract class with abstract methods?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt