0
0
Typescriptprogramming~20 mins

Method overriding with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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());
ATypeScript compilation error due to return type mismatch
B42
C"Hello"
DRuntime error: TypeError
Attempts:
2 left
💡 Hint
Check how TypeScript handles method overriding with different return types in subclasses.
Predict Output
intermediate
2: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"));
AAnimal says bark
BTypeScript error: Parameter type is not compatible
CDog says bark
DRuntime error: Argument type mismatch
Attempts:
2 left
💡 Hint
Look at how the parameter type is narrowed in the subclass method.
🔧 Debug
advanced
2: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;
  }
}
ARuntime error: Type mismatch when calling calculate()
BNo error, code compiles and runs fine
CTypeScript error: Return type 'boolean' is not assignable to return type 'number' in base class
DSyntax error: Missing colon in method declaration
Attempts:
2 left
💡 Hint
Check if the return type in the subclass method matches the base class method.
📝 Syntax
advanced
2: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?
}
Aprocess(data: string): void { console.log(data.toUpperCase()); }
Bprocess(data: number): void { console.log(data); }
Cprocess(data: string): string { return data.toLowerCase(); }
Dprocess(): void { console.log('No data'); }
Attempts:
2 left
💡 Hint
The overriding method must have compatible parameter and return types.
🚀 Application
expert
3: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));
ARuntime error: Method not found
BMoving at 100
CCar moving at 100 km/h
DTypeScript error: Parameter type mismatch in override
Attempts:
2 left
💡 Hint
Consider how TypeScript resolves method calls with union types and overriding.