What does it mean to extend a class in TypeScript?
Extending a class means creating a new class that inherits properties and methods from an existing class. This helps reuse code and add new features.
Click to reveal answer
beginner
How do you specify the type of a class property when extending a class?
You declare the property with its type in the class definition. When extending, you can keep the same type or specify a more specific type if needed.
Click to reveal answer
beginner
What is the syntax to extend a class named <code>Animal</code> in TypeScript?
Use <code>class Dog extends Animal { }</code> to create a new class <code>Dog</code> that inherits from <code>Animal</code>.
Click to reveal answer
intermediate
Can a subclass override a method from its superclass in TypeScript? How?
Yes, a subclass can provide its own version of a method by defining a method with the same name. This replaces the superclass method when called on the subclass instance.
Click to reveal answer
intermediate
How do you call the constructor of a superclass inside a subclass in TypeScript?
Use the <code>super()</code> function inside the subclass constructor to call the superclass constructor and pass any required arguments.
Click to reveal answer
What keyword is used to create a class that inherits from another class in TypeScript?
Ainherits
Bimplements
Csuper
Dextends
✗ Incorrect
The keyword extends is used to create a subclass that inherits from a superclass.
How do you specify the type of a property in a TypeScript class?
ApropertyName: type;
BpropertyName = type;
Ctype propertyName;
DpropertyName -> type;
✗ Incorrect
In TypeScript, you specify a property type using propertyName: type; syntax.
Which method allows a subclass to use the constructor of its superclass?
Athis()
Bsuper()
Cparent()
Dbase()
✗ Incorrect
The super() function calls the superclass constructor inside the subclass constructor.
If a subclass defines a method with the same name as its superclass, what happens?
AThe subclass method overrides the superclass method.
BBoth methods run automatically.
CThe superclass method is called instead.
DAn error occurs.
✗ Incorrect
The subclass method replaces the superclass method when called on subclass instances.
Can a subclass add new properties not present in the superclass?
ANo, subclasses must have the same properties.
BOnly if the superclass allows it.
CYes, subclasses can add new properties.
DOnly if the subclass is abstract.
✗ Incorrect
Subclasses can add new properties and methods beyond what the superclass has.
Explain how extending classes works in TypeScript and why it is useful.
Think about how a new class can get features from an existing class.
You got /5 concepts.
Describe how to override a method in a subclass and how to call the superclass constructor.
Focus on how subclasses change behavior and initialize.
You got /4 concepts.
Practice
(1/5)
1. What does the extends keyword do in TypeScript class definitions?
easy
A. It imports a module into the class.
B. It defines a new interface for a class.
C. It creates a new class that inherits properties and methods from an existing class.
D. It declares a variable inside a class.
Solution
Step 1: Understand the role of extends
The extends keyword 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 what extends does.
Final Answer:
It creates a new class that inherits properties and methods from an existing class. -> Option C
Quick Check:
extends means 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
A. class Dog extends Animal {}
B. class Dog inherits Animal {}
C. class Dog implements Animal {}
D. class Dog import Animal {}
Solution
Step 1: Recall correct syntax for class extension
In TypeScript, the keyword to extend a class is extends, so the syntax is class Dog extends Animal {}.
Step 2: Identify incorrect options
inherits is not a TypeScript keyword, implements is for interfaces, and import is for modules.
Final Answer:
class Dog extends Animal {} -> Option A
Quick Check:
Use extends to 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
A. Hello, undefined
B. Hello, Anna
C. Error: super() not called
D. Hello, Student
Solution
Step 1: Understand class inheritance and constructor call
The Student class extends Person and calls super(name) in its constructor, so the name property is set correctly.
Step 2: Check method call and output
Calling s.greet() uses the inherited method, which returns Hello, Anna.
Final Answer:
Hello, Anna -> Option B
Quick 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
A. Missing call to super() in Car constructor
B. Cannot assign number to wheels property
C. Class Car should not extend Vehicle
D. Constructor syntax is incorrect
Solution
Step 1: Check constructor in subclass
The Car class constructor assigns this.wheels = 4 but does not call super(), which is required before using this.
Step 2: Understand TypeScript rules for subclass constructors
In TypeScript, a subclass constructor must call super() before accessing this. Missing this causes a compile error.
Final Answer:
Missing call to super() in Car constructor -> Option A
Quick 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
A. class Employee extends Person {
department: string;
constructor(name: string, department: string) {
this.department = department;
}
}
B. class Employee extends Person {
department: string;
constructor(department: string) {
this.department = department;
}
}
C. class Employee extends Person {
department: string;
constructor(name: string) {
super(name);
}
}
D. class Employee extends Person {
department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
}
Solution
Step 1: Add department property and constructor parameters
To add department, declare it and add it as a parameter in the constructor along with name.
Step 2: Call super(name) and assign department
Call super(name) to set the inherited name property, then assign this.department = department.
Final Answer:
class Employee extends Person {
department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
} -> Option D
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.