Extending classes with types helps you create new classes based on existing ones. This saves time and keeps your code organized.
Extending classes with types in Typescript
Introduction
Syntax
Typescript
class ChildClass extends ParentClass { // new properties or methods }
The extends keyword means the new class gets all features of the parent class.
You can add new properties or methods inside the child class.
Examples
Dog that extends Animal. It changes the speak method.Typescript
class Animal { name: string; constructor(name: string) { this.name = name; } speak() { console.log(`${this.name} makes a sound.`); } } class Dog extends Animal { speak() { console.log(`${this.name} barks.`); } }
Car extends Vehicle. It uses super to call the parent constructor and adds a new property.Typescript
class Vehicle { wheels: number; constructor(wheels: number) { this.wheels = wheels; } } class Car extends Vehicle { brand: string; constructor(brand: string) { super(4); this.brand = brand; } }
Sample Program
This program creates a Person class and a Student class that extends it. The student can greet and study.
Typescript
class Person { name: string; constructor(name: string) { this.name = name; } greet() { console.log(`Hello, my name is ${this.name}.`); } } class Student extends Person { grade: number; constructor(name: string, grade: number) { super(name); this.grade = grade; } study() { console.log(`${this.name} is studying for grade ${this.grade}.`); } } const student = new Student("Alice", 10); student.greet(); student.study();
Important Notes
Use super() in the child class constructor to call the parent class constructor.
Child classes inherit all public and protected members from the parent class.
You can override methods in the child class to change behavior.
Summary
Extending classes lets you build new classes from existing ones.
Use extends to inherit properties and methods.
Call super() to run the parent constructor inside the child.
Practice
1. What does the
extends keyword do in TypeScript class definitions?easy
Solution
Step 1: Understand the role of
Theextendsextendskeyword is used to create a new class that inherits from an existing class, gaining its properties and methods.Step 2: Differentiate from other keywords
Other options describe unrelated concepts: interfaces, variables, or modules, which are not whatextendsdoes.Final Answer:
It creates a new class that inherits properties and methods from an existing class. -> Option CQuick Check:
extendsmeans inheritance [OK]
Hint: Remember: extends means 'is a' relationship in classes [OK]
Common Mistakes:
- Confusing extends with interface implementation
- Thinking extends imports modules
- Using extends to declare variables
2. Which of the following is the correct syntax to extend a class named
Animal in TypeScript?easy
Solution
Step 1: Recall correct syntax for class extension
In TypeScript, the keyword to extend a class isextends, so the syntax isclass Dog extends Animal {}.Step 2: Identify incorrect options
inheritsis not a TypeScript keyword,implementsis for interfaces, andimportis for modules.Final Answer:
class Dog extends Animal {} -> Option AQuick Check:
Useextendsto inherit classes [OK]
Hint: Use 'extends' keyword to inherit classes, not 'inherits' or 'implements' [OK]
Common Mistakes:
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Trying to import classes inside class declaration
3. What will be the output of this TypeScript code?
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
const s = new Student('Anna');
console.log(s.greet());medium
Solution
Step 1: Understand class inheritance and constructor call
TheStudentclass extendsPersonand callssuper(name)in its constructor, so thenameproperty is set correctly.Step 2: Check method call and output
Callings.greet()uses the inherited method, which returnsHello, Anna.Final Answer:
Hello, Anna -> Option BQuick Check:
super() sets parent properties correctly [OK]
Hint: Always call super() in child constructor to set parent properties [OK]
Common Mistakes:
- Forgetting to call super() causes errors
- Expecting child's own greet method without override
- Assuming name is undefined without super()
4. Identify the error in this TypeScript code:
class Vehicle {
wheels: number;
constructor(wheels: number) {
this.wheels = wheels;
}
}
class Car extends Vehicle {
constructor() {
this.wheels = 4;
}
}medium
Solution
Step 1: Check constructor in subclass
TheCarclass constructor assignsthis.wheels = 4but does not callsuper(), which is required before usingthis.Step 2: Understand TypeScript rules for subclass constructors
In TypeScript, a subclass constructor must callsuper()before accessingthis. Missing this causes a compile error.Final Answer:
Missing call to super() in Car constructor -> Option AQuick Check:
Always call super() before this in subclass constructor [OK]
Hint: Call super() first in subclass constructor before using this [OK]
Common Mistakes:
- Assigning to this before super() call
- Forgetting super() call causes runtime error
- Thinking super() is optional in subclass constructor
5. Given these classes, how can you extend
Employee to add a department property and keep the name property from Person? class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
// Add department property here
}hard
Solution
Step 1: Add department property and constructor parameters
To adddepartment, declare it and add it as a parameter in the constructor along withname.Step 2: Call super(name) and assign department
Callsuper(name)to set the inheritednameproperty, then assignthis.department = department.Final Answer:
class Employee extends Person { department: string; constructor(name: string, department: string) { super(name); this.department = department; } } -> Option DQuick Check:
Use super() for parent props and assign new props in constructor [OK]
Hint: Call super(name) then assign new properties in constructor [OK]
Common Mistakes:
- Not calling super() in constructor
- Missing department parameter in constructor
- Assigning this.department before super() call
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
