0
0
Typescriptprogramming~10 mins

Abstract methods in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an abstract method named display.

Typescript
abstract class Vehicle {
  abstract [1](): void;
}
Drag options to blanks, or click blank then click option'
Aprint
Bshow
Cdisplay
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than display.
Trying to provide a method body in the abstract method.
2fill in blank
medium

Complete the code to make Vehicle an abstract class.

Typescript
abstract class [1] {
  abstract display(): void;
}
Drag options to blanks, or click blank then click option'
AVehicle
BCar
CTransport
DMachine
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different class name than Vehicle.
Not declaring the class as abstract (though not asked here).
3fill in blank
hard

Fix the error in the subclass by correctly implementing the abstract method display.

Typescript
abstract class Vehicle {
  abstract display(): void;
}

class Car extends Vehicle {
  [1] display(): void {
    console.log('Car display');
  }
}
Drag options to blanks, or click blank then click option'
Aoverride
Babstract
Cprivate
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using abstract keyword in subclass method implementation.
Using private which restricts access and causes errors.
4fill in blank
hard

Fill both blanks to declare an abstract method calculateSpeed that returns a number.

Typescript
abstract class Vehicle {
  abstract [1](): [2];
}
Drag options to blanks, or click blank then click option'
AcalculateSpeed
Bstring
Cnumber
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong return type like string or void.
Using a different method name.
5fill in blank
hard

Fill all three blanks to implement the abstract method calculateSpeed in subclass Bike that returns a number.

Typescript
abstract class Vehicle {
  abstract calculateSpeed(): number;
}

class Bike extends Vehicle {
  [1] [2](): [3] {
    return 50;
  }
}
Drag options to blanks, or click blank then click option'
Apublic
BcalculateSpeed
Cnumber
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type instead of number.
Using wrong method name or missing access modifier.