Why inheritance needs types in Typescript - Performance Analysis
When using inheritance in TypeScript, understanding how types affect performance helps us write better code.
We want to see how the program's work grows when inheritance and types interact.
Analyze the time complexity of the following code snippet.
class Animal {
speak() {
return "...";
}
}
class Dog extends Animal {
speak() {
return "Woof!";
}
}
function makeSpeak(animal: Animal) {
return animal.speak();
}
const dog = new Dog();
makeSpeak(dog);
This code shows a base class and a child class overriding a method, with a function using the base type.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
speakmethod on an object typed asAnimal. - How many times: Once per call to
makeSpeak, but could be many times if called repeatedly.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls |
| 100 | 100 method calls |
| 1000 | 1000 method calls |
Pattern observation: The number of operations grows directly with how many times the method is called.
Time Complexity: O(n)
This means the work grows linearly with the number of method calls on inherited types.
[X] Wrong: "Inheritance with types makes the program slower because of extra checks."
[OK] Correct: TypeScript types are erased at runtime, so inheritance does not add overhead by itself; the cost depends on how many times methods run.
Understanding how inheritance and types affect performance helps you explain design choices clearly and write efficient code in real projects.
"What if we changed the method to call another method inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand inheritance purpose
Inheritance allows a child class to reuse code from a parent class.Step 2: Role of types in inheritance
Types ensure the child class matches the parent's structure, preventing mistakes.Final Answer:
To ensure the child class follows the structure of the parent class -> Option AQuick Check:
Types keep inheritance safe = A [OK]
- Thinking types speed up code execution
- Confusing inheritance with multiple inheritance
- Believing constructors are avoided by types
Dog that inherits from class Animal in TypeScript?Solution
Step 1: Recall TypeScript inheritance syntax
TypeScript uses the keywordextendsto inherit from a class.Step 2: Check each option
Only class Dog extends Animal {} usesextendscorrectly; others use invalid syntax.Final Answer:
class Dog extends Animal {} -> Option DQuick Check:
Inheritance uses 'extends' keyword = A [OK]
- Using 'inherits' instead of 'extends'
- Using ':' like other languages (e.g., C#)
- Omitting 'extends' keyword
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());Solution
Step 1: Understand class inheritance and method overriding
Dog inherits from Animal and overrides the speak() method.Step 2: Check the output of dog.speak()
dog.speak() calls Dog's speak(), returning 'Buddy barks.'Final Answer:
Buddy barks. -> Option BQuick Check:
Overridden method runs = D [OK]
- Expecting parent method output
- Thinking speak method is missing
- Assuming name is undefined
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
}
class Dog extends Animal {
constructor() {
super();
}
}Solution
Step 1: Check Animal constructor parameters
Animal's constructor requires a string argumentname.Step 2: Check Dog constructor call to super()
Dog callssuper()without arguments, causing an error.Final Answer:
Missing argument in super() call -> Option AQuick Check:
super() needs required parameters = B [OK]
- Calling super() without arguments when parent needs them
- Thinking child class can't have constructor
- Confusing property access modifiers
Rectangle that inherits from Shape. The Shape class requires a color string property. How should you define the Rectangle constructor to properly use inheritance and types in TypeScript?Solution
Step 1: Understand parent constructor requirements
Shape requires acolorstring in its constructor.Step 2: Define Rectangle constructor with all needed parameters
Rectangle must acceptcolorand pass it tosuper(color), plus its own properties.Step 3: Check options for correct super call and property assignments
Only constructor(color: string, width: number, height: number) { super(color); this.width = width; this.height = height; } correctly callssuper(color)and assigns width and height.Final Answer:
constructor(color: string, width: number, height: number) { super(color); this.width = width; this.height = height; } -> Option CQuick Check:
Pass parent args via super() = C [OK]
- Omitting super() call or its arguments
- Assigning parent properties directly without super()
- Missing child class properties in constructor
Ready to prove it?
Pass the test and this pattern moves unknown
