Super keyword behavior in Typescript - Time & Space Complexity
Let's explore how using the super keyword affects the time it takes for a program to run.
We want to know how calling methods from a parent class scales as our program grows.
Analyze the time complexity of the following code snippet.
class Animal {
speak() {
console.log('Animal speaks');
}
}
class Dog extends Animal {
speak() {
super.speak();
console.log('Dog barks');
}
}
const dog = new Dog();
dog.speak();
This code shows a class Dog that calls its parent Animal method using super.speak().
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
super.speak()inside theDogclass method. - How many times: Exactly once per
dog.speak()call.
Since super.speak() calls a single parent method, the work done does not increase with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls, each calls super.speak() once |
| 100 | 100 calls, each calls super.speak() once |
| 1000 | 1000 calls, each calls super.speak() once |
Pattern observation: The number of operations grows directly with how many times the method is called, but each call does a fixed amount of work.
Time Complexity: O(n)
This means the total time grows linearly with how many times you call the method, but each call itself is simple and constant time.
[X] Wrong: "Using super makes the method slower by a lot because it calls another method."
[OK] Correct: Calling super just runs one extra method, which is a fixed cost and does not multiply with input size inside that call.
Understanding how super works helps you explain class inheritance clearly and shows you know how method calls affect performance in real code.
"What if the parent method called by super itself called another method multiple times? How would that affect the time complexity?"
Practice
What does the super keyword do in TypeScript classes?
Solution
Step 1: Understand the role of
Thesupersuperkeyword is used in child classes to refer to their parent class.Step 2: Identify what
It allows calling the parent class constructor or methods from the child class.superaccessesFinal Answer:
It allows a child class to access and call methods or constructors from its parent class. -> Option CQuick Check:
superaccesses parent features = A [OK]
super connects child to parent class features [OK]- Thinking
supercreates new instances - Confusing
superwith static method declaration - Assuming
superdeclares properties
Which of the following is the correct way to call a parent class constructor inside a child class constructor?
class Parent { constructor(name: string) {} }
class Child extends Parent {
constructor(name: string) {
?
}
}Solution
Step 1: Recall syntax for calling parent constructor
In TypeScript, inside a child constructor,super()calls the parent constructor.Step 2: Match correct syntax
The correct call issuper(name);to pass the argument to the parent.Final Answer:
super(name); -> Option AQuick Check:
Parent constructor call = super(name) [OK]
super(args) to call parent constructor inside child [OK]- Using
this.super()which is invalid - Calling parent class name like a function
- Trying
super.constructor()which is incorrect
What will be the output of the following TypeScript code?
class Animal {
speak() { return 'Animal sound'; }
}
class Dog extends Animal {
speak() { return super.speak() + ' and Bark'; }
}
const dog = new Dog();
console.log(dog.speak());Solution
Step 1: Understand method overriding and
ThesuperDogclass overridesspeak()and callssuper.speak()to get the parent method's return.Step 2: Combine parent and child method outputs
super.speak()returns "Animal sound", then " and Bark" is added.Final Answer:
"Animal sound and Bark" -> Option DQuick Check:
super method call adds parent output = D [OK]
- Expecting only 'Bark' without parent sound
- Thinking it causes an error
- Ignoring
supercall inside method
Identify the error in this TypeScript code snippet:
class Base {
constructor() { console.log('Base constructor'); }
}
class Derived extends Base {
constructor() {
console.log('Derived constructor');
super();
}
}Solution
Step 1: Recall constructor rules in derived classes
In TypeScript,super()must be called before any other statement in a derived class constructor.Step 2: Identify the error in code order
The code callsconsole.logbeforesuper(), which is invalid and causes a runtime error.Final Answer:
Callingsuper()after a statement in constructor causes error. -> Option BQuick Check:
super()must be first in constructor [OK]
super() first in child constructor [OK]- Calling
super()after other code - Thinking
super()can be omitted - Assuming no error if
super()is late
Given the following classes, what will console.log(new C().getValue()); print?
class A {
value = 1;
getValue() { return this.value; }
}
class B extends A {
value = 2;
getValue() { return super.getValue() + this.value; }
}
class C extends B {
value = 3;
getValue() { return super.getValue() + this.value; }
}Solution
Step 1: Evaluate
C.getValue()callC.getValue()callssuper.getValue()(which isB.getValue()) plusthis.value(3).Step 2: Evaluate
B.getValue()callB.getValue()callssuper.getValue()(which isA.getValue()) plusthis.value(3, becausethisis instance ofC).Step 3: Evaluate
A.getValue()callA.getValue()returnsthis.value, which is 3 (fromCinstance).Step 4: Calculate total
So,A.getValue()= 3,B.getValue()= 3 + 3 = 6,C.getValue()= 6 + 3 = 9.Step 5: Reconsider
However, each class defines its ownvalueproperty shadowingvalueproperty. Butthis.valuealways refers toC'svalue(3) becausethisis the instance ofC.Final Answer:
9 -> Option AQuick Check:
super calls chain add values = 9 [OK]
this refers to child instance, not parent [OK]- Assuming
this.valuechanges in each class - Ignoring
thisbinding to child instance - Adding only parent values without child
Ready to prove it?
Pass the test and this pattern moves unknown
