Discover how a simple keyword can save you from rewriting code and bugs!
Why Super keyword behavior in Typescript? - Purpose & Use Cases
Imagine you have a family recipe book, and you want to add a new recipe that builds on your grandma's original recipe. Without a clear way to refer back to her recipe, you'd have to rewrite or copy it each time, risking mistakes or inconsistencies.
Manually rewriting or copying the original recipe every time is slow and error-prone. You might forget an important step or ingredient, and updating the original recipe means changing it everywhere manually. This leads to confusion and bugs in your code.
The super keyword acts like a direct link to the original recipe. It lets you easily call or extend the behavior of a parent class method without rewriting it. This keeps your code clean, consistent, and easy to maintain.
class Dog { speak() { return 'Woof!'; } } class Puppy extends Dog { speak() { return 'Woof! Woof!'; // repeats parent logic manually } }
class Dog { speak() { return 'Woof!'; } } class Puppy extends Dog { speak() { return super.speak() + ' Woof!'; } }
It enables you to build on existing behaviors easily, making your code more reusable and less error-prone.
When creating a new type of user interface button that behaves like a standard button but with extra animation, super lets you reuse the standard button's click behavior and add your animation on top.
Super keyword helps call parent class methods easily.
It avoids rewriting code and reduces mistakes.
Makes extending and maintaining code simpler and cleaner.
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
