0
0
Typescriptprogramming~10 mins

Extending classes with types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extending classes with types
Define Base Class
Define Derived Class extends Base
Derived inherits properties/methods
Create instance of Derived
Access inherited and own members
This flow shows how a derived class extends a base class, inheriting its properties and methods, and how instances of the derived class can use both.
Execution Sample
Typescript
class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  speak() { return `${this.name} makes a sound.`; }
}

class Dog extends Animal {
  speak() { return `${this.name} barks.`; }
}

const dog = new Dog("Buddy");
console.log(dog.speak());
Defines a base class Animal and a derived class Dog that overrides speak(). Creates a Dog instance and calls speak().
Execution Table
StepActionEvaluationResult
1Define class Animal with property name and method speakN/AAnimal class created
2Define class Dog extending Animal, override speak methodN/ADog class created, inherits Animal
3Create instance dog = new Dog("Buddy")Dog constructor calls Animal constructordog.name = "Buddy"
4Call dog.speak()Dog's speak() overrides Animal's"Buddy barks."
5Output resultconsole.log printsBuddy barks.
💡 Execution ends after printing the dog's speak output.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
dogundefined{name: "Buddy"}N/A{name: "Buddy"}
Key Moments - 2 Insights
Why does dog.speak() use the Dog version, not Animal's?
Because Dog overrides speak(), so when called on a Dog instance (see execution_table step 4), the Dog version runs.
How does Dog get the name property if it is only defined in Animal?
Dog extends Animal, so it inherits Animal's properties. The Animal constructor sets name when Dog calls super(name) (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the value of dog.name after creation?
Aundefined
B"Dog"
C"Buddy"
Dnull
💡 Hint
Check variable_tracker row for dog after step 3.
At which step does the Dog class override the speak method?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table step 2 where Dog class is defined.
If Dog did not override speak(), what would dog.speak() output at step 4?
A"Buddy barks."
B"Buddy makes a sound."
CError: speak not found
D"Dog makes a sound."
💡 Hint
Refer to execution_table step 4 and how overriding works.
Concept Snapshot
class Base {
  prop: type;
  constructor(...) { ... }
  method() { ... }
}
class Derived extends Base {
  method() { ... } // override
}

Derived inherits Base's props and methods, can override them.
Full Transcript
This example shows how in TypeScript a class can extend another class to inherit its properties and methods. The base class Animal has a name property and a speak method. The derived class Dog extends Animal and overrides the speak method to provide a different message. When we create a Dog instance with name "Buddy" and call speak, the Dog's version runs, printing "Buddy barks.". This demonstrates inheritance and method overriding in classes with types.