0
0
Typescriptprogramming~10 mins

Method overriding with types 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 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'
Aboolean
Bstring
Cvoid
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different return type like number or void causes a type error.
2fill in blank
medium

Complete 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'
Adistance: number
Bdistance: boolean
Cdistance: string
Ddistance
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the parameter type to string or boolean causes a type mismatch.
3fill in blank
hard

Fix 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'
Astring
Bboolean
Cnumber
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the return type to string or number causes a type error.
4fill in blank
hard

Fill 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'
Aa: number
Bb: string
Cb: number
Da: string
Attempts:
3 left
💡 Hint
Common Mistakes
Using string types for parameters causes type errors.
5fill in blank
hard

Fill 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'
Astring
Bmessage: string
Clevel: number
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Changing parameter types or return type causes type errors.