Bird
Raised Fist0
Typescriptprogramming~10 mins

Method overriding with types in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Method overriding with types
Define Base Class with Method
↓
Define Derived Class
↓
Override Method in Derived Class
↓
Call Method on Derived Instance
↓
Derived Method Executes
↓
Return Result with Derived Type
Shows how a derived class replaces a base class method with a new version that can have a more specific type.
Execution Sample
Typescript
class Animal {
  speak(): string {
    return "Animal sound";
  }
}

class Dog extends Animal {
  speak(): "bark" {
    return "bark";
  }
}

const pet: Animal = new Dog();
console.log(pet.speak());
Defines a base class Animal with a speak method, then a Dog class overrides speak with a more specific return type.
Execution Table
StepActionEvaluationResult
1Define class Animal with speak()Method returns stringAnimal.speak() returns "Animal sound"
2Define class Dog extends AnimalOverride speak() with return type "bark"Dog.speak() returns "bark"
3Create pet as Animal type but instance of Dogpet is Animal reference to Dog objectpet points to Dog instance
4Call pet.speak()Calls Dog's overridden speak()Returns "bark"
5Print resultOutput to consolebark
💡 Execution stops after printing the overridden method's return value.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
petundefinedDog instance (as Animal)Dog instance (as Animal)Dog instance (as Animal)
Key Moments - 2 Insights
Why does pet.speak() call Dog's method even though pet is typed as Animal?
Because the actual object is a Dog instance, method calls use the object's method, not the variable's type (see execution_table step 4).
Can the overridden method in Dog have a more specific return type than in Animal?
Yes, TypeScript allows the derived method to return a subtype or literal type (see Dog.speak() returns "bark" vs Animal.speak() returns string).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does pet.speak() return?
A"bark"
B"Animal sound"
Cundefined
DError
💡 Hint
Check the 'Result' column at step 4 in execution_table.
At which step is the pet variable assigned a Dog instance?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at variable_tracker and execution_table step 3.
If Dog.speak() returned a number instead of "bark", what would happen?
AThe base class method would be called instead
BIt would work fine
CTypeScript would give a type error
DThe program would crash
💡 Hint
Method overriding requires compatible return types (see key_moments about return types).
Concept Snapshot
Method overriding lets a derived class replace a base class method.
The derived method can have a more specific return type.
Calls use the actual object's method, not the variable's type.
TypeScript checks that overridden methods keep compatible types.
This enables polymorphism with type safety.
Full Transcript
This example shows method overriding with types in TypeScript. We start by defining a base class Animal with a method speak() that returns a string. Then we define a derived class Dog that overrides speak() to return the specific string literal "bark". We create a variable pet typed as Animal but assign it a Dog instance. When we call pet.speak(), the Dog's overridden method runs, returning "bark". This works because method calls use the actual object's method, not the variable's type. TypeScript allows the derived method to have a more specific return type, ensuring type safety. The execution table traces each step, showing how the variable pet changes and how the method call resolves. Key moments clarify why the overridden method runs and how return types work. The visual quiz tests understanding of these points. This teaches how method overriding with types enables flexible and safe polymorphism in TypeScript.

Practice

(1/5)
1.

What does method overriding mean in TypeScript?

easy
A. A child class provides its own version of a method from the parent class.
B. A child class deletes a method from the parent class.
C. A parent class changes a method from the child class.
D. A method is called without any arguments.

Solution

  1. Step 1: Understand method overriding

    Method overriding means a child class replaces a method defined in its parent class with its own version.
  2. Step 2: Identify correct description

    A child class provides its own version of a method from the parent class. correctly describes this behavior as the child class providing its own method version.
  3. Final Answer:

    A child class provides its own version of a method from the parent class. -> Option A
  4. Quick Check:

    Method overriding = child method replaces parent method [OK]
Hint: Child class method replaces parent method behavior [OK]
Common Mistakes:
  • Thinking parent changes child methods
  • Confusing overriding with deleting methods
  • Believing overriding means calling method without arguments
2.

Which of the following is the correct syntax for overriding a method greet() in a child class in TypeScript?

class Parent {
  greet(): string {
    return "Hello";
  }
}

class Child extends Parent {
  ?
}
easy
A. greet() { return 10; }
B. greet(): number { return 5; }
C. greet(name: string): string { return `Hi, ${name}`; }
D. greet(): string { return "Hi"; }

Solution

  1. Step 1: Check method signature compatibility

    The child method must have the same name and compatible return type as the parent method. Parent returns string and takes no parameters.
  2. Step 2: Identify correct override

    greet(): string { return "Hi"; } matches the parent's method signature exactly (no parameters, returns string). Others differ in return type or parameters.
  3. Final Answer:

    greet(): string { return "Hi"; } -> Option D
  4. Quick Check:

    Override method signature must match parent [OK]
Hint: Override method must match parent's name and return type [OK]
Common Mistakes:
  • Changing return type to number
  • Adding parameters not in parent method
  • Omitting return type causing implicit any
3.

What will be the output of this TypeScript code?

class Animal {
  speak(): string {
    return "Animal sound";
  }
}

class Dog extends Animal {
  speak(): string {
    return "Bark";
  }
}

const pet: Animal = new Dog();
console.log(pet.speak());
medium
A. "Bark"
B. Error: Method not found
C. undefined
D. "Animal sound"

Solution

  1. Step 1: Understand polymorphism with method overriding

    Even though the variable type is Animal, the actual object is Dog. The overridden method in Dog is called.
  2. Step 2: Determine method output

    Dog's speak() returns "Bark", so console.log prints "Bark".
  3. Final Answer:

    "Bark" -> Option A
  4. Quick Check:

    Overridden method runs on actual object type [OK]
Hint: Actual object method runs, not variable type method [OK]
Common Mistakes:
  • Thinking parent method runs due to variable type
  • Expecting undefined because of type mismatch
  • Assuming runtime error for overridden method
4.

Identify the error in this TypeScript code that tries to override a method:

class Vehicle {
  move(distance: number): void {
    console.log(`Moved ${distance} meters`);
  }
}

class Car extends Vehicle {
  move(distance: string): void {
    console.log(`Car moved ${distance}`);
  }
}
medium
A. Child class cannot override methods from parent class.
B. Return type void is not allowed in overriding.
C. Parameter type in child method must match parent method's parameter type.
D. Method name must be different in child class.

Solution

  1. Step 1: Check method signature compatibility

    Parent method move() takes a number parameter. Child method move() takes a string parameter, which is incompatible.
  2. Step 2: Identify correct error cause

    Parameter type in child method must match parent method's parameter type. correctly states this.
  3. Final Answer:

    Parameter type in child method must match parent method's parameter type. -> Option C
  4. Quick Check:

    Parameter types must be compatible in overriding [OK]
Hint: Parameter types must match exactly for overriding [OK]
Common Mistakes:
  • Changing parameter types in child method
  • Thinking return type void is invalid
  • Believing method names must differ
5.

Given these classes, which override is valid and why?

class Base {
  calculate(value: number): number {
    return value * 2;
  }
}

class Derived extends Base {
  calculate(value: number): number | string {
    if (value > 10) return "Too large";
    return value * 3;
  }
}
hard
A. Valid because return type includes parent's return type (number) plus string.
B. Invalid because return type must exactly match parent's return type.
C. Invalid because method name changed.
D. Valid because child method can return any type.

Solution

  1. Step 1: Check return type compatibility in overriding

    Parent method returns number only. Child method returns number or string, which is not compatible because string is not assignable to number.
  2. Step 2: Understand TypeScript's strict method overriding rules

    TypeScript requires the child method's return type to be assignable to the parent's return type, not broader. Invalid because return type must exactly match parent's return type. correctly states this.
  3. Final Answer:

    Invalid because return type must exactly match parent's return type. -> Option B
  4. Quick Check:

    Return type must be compatible, not broader [OK]
Hint: Child return type must be assignable to parent's return type [OK]
Common Mistakes:
  • Allowing broader return types in child method
  • Ignoring method name consistency
  • Assuming any return type is allowed in override
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard