Challenge - 5 Problems
Master of Method Overriding with Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of overridden method with different return types
What is the output of the following TypeScript code when calling
child.getValue()?Typescript
class Parent { getValue(): number { return 42; } } class Child extends Parent { getValue(): string { return "Hello"; } } const child = new Child(); console.log(child.getValue());
Attempts:
2 left
💡 Hint
Check how TypeScript handles method overriding with different return types in subclasses.
✗ Incorrect
TypeScript requires overriding methods to have covariant return types: the subclass return type must be assignable to the base class return type. String is not assignable to number, causing a compilation error.
❓ Predict Output
intermediate2:00remaining
Output when overriding method with covariant parameter types
What will be the output of this TypeScript code snippet?
Typescript
class Animal { speak(sound: string): string { return `Animal says ${sound}`; } } class Dog extends Animal { speak(sound: "bark" | "woof"): string { return `Dog says ${sound}`; } } const dog = new Dog(); console.log(dog.speak("bark"));
Attempts:
2 left
💡 Hint
Look at how the parameter type is narrowed in the subclass method.
✗ Incorrect
TypeScript requires contravariant parameters for overriding methods: the subclass parameter type must be a supertype of the base parameter type. '"bark" | "woof"' is narrower (subtype), so incompatible, causing a TypeScript error.
🔧 Debug
advanced2:00remaining
Identify the error in method overriding with incompatible return types
Which option correctly identifies the error in this TypeScript code?
Typescript
class Base { calculate(): number { return 10; } } class Derived extends Base { calculate(): boolean { return true; } }
Attempts:
2 left
💡 Hint
Check if the return type in the subclass method matches the base class method.
✗ Incorrect
TypeScript enforces that an overriding method must have a return type compatible with the base method. Here, the Derived class returns a boolean while the base returns a number, causing a compilation error.
📝 Syntax
advanced2:00remaining
Which method override syntax is valid in TypeScript?
Given the base class method
process(data: string): void, which subclass method override is syntactically valid?Typescript
class Processor { process(data: string): void { console.log(data); } } class CustomProcessor extends Processor { // Which override is valid? }
Attempts:
2 left
💡 Hint
The overriding method must have compatible parameter and return types.
✗ Incorrect
Option A matches the base method signature exactly and is valid. Option A changes parameter type, causing error. Option A changes return type from void to string, causing error. Option A removes the parameter, causing error.
🚀 Application
expert3:00remaining
Determine the output with method overriding and union types
What is the output of this TypeScript code?
Typescript
class Vehicle { move(speed: number | string): string { return `Moving at ${speed}`; } } class Car extends Vehicle { move(speed: number): string { return `Car moving at ${speed} km/h`; } } const myCar: Vehicle = new Car(); console.log(myCar.move(100));
Attempts:
2 left
💡 Hint
Consider how TypeScript resolves method calls with union types and overriding.
✗ Incorrect
TypeScript requires contravariant parameters for overrides. The Car class uses a narrower parameter type (number vs. number | string), making 'number | string' not assignable to 'number', causing a compilation error.