📖 Scenario: You are building a simple system to manage vehicles. You want to create a base class for a vehicle and then extend it to create a specific type of vehicle, like a car, with extra properties.
🎯 Goal: Learn how to create a base class with types and extend it with a subclass that adds more typed properties.
📋 What You'll Learn
Create a base class called Vehicle with typed properties
Create a subclass called Car that extends Vehicle
Add a new typed property to Car
Create an instance of Car and print its details
💡 Why This Matters
🌍 Real World
Extending classes with types is common when modeling real-world objects that share common features but also have unique details.
💼 Career
Understanding class inheritance and typing is essential for building scalable and maintainable applications in TypeScript, widely used in web development.
Progress0 / 4 steps
1
Create the base class Vehicle
Create a class called Vehicle with two typed properties: make of type string and year of type number. Add a constructor that sets these properties.
Typescript
Hint
Use class Vehicle and define make and year as properties with types. Then add a constructor to set them.
2
Create the subclass Car extending Vehicle
Create a class called Car that extends Vehicle. Add a new typed property called doors of type number. Add a constructor that takes make, year, and doors and calls the base class constructor for make and year.
Typescript
Hint
Use extends Vehicle to create the subclass. Use super(make, year) inside the constructor to call the base class constructor.
3
Create an instance of Car
Create a variable called myCar and assign it a new Car object with make as "Toyota", year as 2020, and doors as 4.
Typescript
Hint
Use new Car("Toyota", 2020, 4) to create the instance and assign it to myCar.
4
Print the details of myCar
Write a console.log statement to print the string: "Make: Toyota, Year: 2020, Doors: 4" using the properties of myCar.
Typescript
Hint
Use a template string with console.log to print the properties of myCar.
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.