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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 5

bookImplementing Abstract Classes

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
some-alt