The super keyword helps you use or extend features from a parent class inside a child class.
Super keyword behavior in Typescript
class Parent { constructor() {} method() {} } class Child extends Parent { constructor() { super(); } method() { super.method(); } }
super() must be called in the child constructor before using this.
You can use super.methodName() to call a specific method from the parent.
class Animal { constructor(name: string) { console.log(`Animal created: ${name}`); } } class Dog extends Animal { constructor(name: string) { super(name); console.log('Dog created'); } }
greet method, then adds extra greeting.class Person { greet() { console.log('Hello from Person'); } } class Student extends Person { greet() { super.greet(); console.log('Hello from Student'); } }
This program shows how super calls the parent constructor and method, then the child adds its own message.
class Vehicle { constructor(public brand: string) {} start() { console.log(`${this.brand} vehicle started.`); } } class Car extends Vehicle { constructor(brand: string, public model: string) { super(brand); } start() { super.start(); console.log(`${this.model} car is ready to go!`); } } const myCar = new Car('Toyota', 'Corolla'); myCar.start();
Always call super() first in the child constructor before using this.
You can use super to access parent methods even if you override them in the child.
super lets child classes use or extend parent class features.
Use super() in constructors to call the parent constructor.
Use super.method() to call a parent method inside an overridden method.
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
