0
0
Typescriptprogramming~10 mins

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

Choose your learning style9 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.