Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to override the method in the subclass.
Typescript
class Animal { speak(): string { return "Generic sound"; } } class Dog extends Animal { speak(): [1] { return "Bark"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different return type like number or void causes a type error.
✗ Incorrect
The overridden method must have the same return type 'string' as in the base class.
2fill in blank
mediumComplete the code to override the method with a parameter type.
Typescript
class Vehicle { move(distance: number): string { return `Moved ${distance} meters`; } } class Car extends Vehicle { move([1]): string { return `Car moved ${distance} meters`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the parameter type to string or boolean causes a type mismatch.
✗ Incorrect
The parameter type must match the base class method's parameter type 'number'.
3fill in blank
hardFix the error in the method override by correcting the return type.
Typescript
class Printer { print(): void { console.log("Printing..."); } } class ColorPrinter extends Printer { print(): [1] { console.log("Printing in color..."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the return type to string or number causes a type error.
✗ Incorrect
The overridden method must have the same return type 'void' as the base class method.
4fill in blank
hardFill both blanks to correctly override the method with parameter and return types.
Typescript
class Calculator { calculate(a: number, b: number): number { return a + b; } } class AdvancedCalculator extends Calculator { calculate([1], [2]): number { return a * b; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string types for parameters causes type errors.
✗ Incorrect
Both parameters must have the same types as in the base class method: 'a: number' and 'b: number'.
5fill in blank
hardFill all three blanks to override the method with parameter types and return type.
Typescript
class Logger { log(message: string, level: number): [1] { return `${level}: ${message}`; } } class CustomLogger extends Logger { log([2], [3]): string { return `Level ${level} - ${message}`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Changing parameter types or return type causes type errors.
✗ Incorrect
The return type and parameter types must match the base class method: return type 'string', parameters 'message: string' and 'level: number'.