Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 5

bookImplementing Abstract Classes

Deslize para mostrar o menu

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
some-alt