Abstract classes in Typescript - Time & Space Complexity
We want to understand how the time it takes to run code with abstract classes changes as the input grows.
Specifically, how does using abstract classes affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
abstract class Shape {
abstract area(): number;
}
class Square extends Shape {
constructor(private side: number) { super(); }
area() { return this.side * this.side; }
}
function totalArea(shapes: Shape[]): number {
let sum = 0;
for (const shape of shapes) {
sum += shape.area();
}
return sum;
}
This code defines an abstract class and calculates the total area of many shapes by calling their area method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of shapes and calling
area()on each. - How many times: Once for each shape in the input array.
Each shape requires one call to area(), so the total steps grow directly with the number of shapes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to area() and 10 loop steps |
| 100 | 100 calls to area() and 100 loop steps |
| 1000 | 1000 calls to area() and 1000 loop steps |
Pattern observation: The work grows evenly as the number of shapes increases.
Time Complexity: O(n)
This means the time to calculate total area grows in a straight line with the number of shapes.
[X] Wrong: "Using abstract classes makes the code slower because of extra overhead."
[OK] Correct: The abstract class itself does not add extra loops or repeated work; it just defines a structure. The main cost depends on how many shapes you process.
Understanding how abstract classes affect time helps you explain design choices clearly and shows you know how structure relates to performance.
"What if each shape's area() method did a complex calculation with its own loops? How would the time complexity change?"
Practice
abstract class in TypeScript?Solution
Step 1: Understand abstract class instantiation rules
Abstract classes cannot be instantiated directly, so you cannot create objects from them usingnew.Step 2: Compare other options
Abstract classes can have implemented methods, are different from interfaces, and cannot be instantiated withnew.Final Answer:
You cannot create an instance of an abstract class directly. -> Option CQuick Check:
Abstract class instantiation = not allowed [OK]
- Thinking abstract classes are interfaces
- Assuming abstract classes can't have implemented methods
- Trying to instantiate abstract classes directly
Solution
Step 1: Recall TypeScript abstract class syntax
The correct syntax places the keywordabstractbeforeclass, followed by the class name.Step 2: Check each option
Only abstract class Vehicle {} correctly usesabstract class Vehicle {}. Others have incorrect keyword order.Final Answer:
abstract class Vehicle {} -> Option AQuick Check:
Abstract class syntax =abstract class Name {}[OK]
- Placing 'abstract' after 'class'
- Mixing keyword order
- Omitting 'abstract' keyword
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log('Moving');
}
}
class Dog extends Animal {
makeSound(): void {
console.log('Bark');
}
}
const dog = new Dog();
dog.makeSound();
dog.move();Solution
Step 1: Understand class inheritance and method calls
The classDogextendsAnimaland implementsmakeSound(). Callingdog.makeSound()prints 'Bark'.Step 2: Check the
Themove()method callmove()method is implemented inAnimaland prints 'Moving'. Callingdog.move()prints 'Moving'.Final Answer:
Bark Moving -> Option DQuick Check:
Subclass methods run, abstract class methods inherited [OK]
- Thinking abstract class instance is created
- Confusing method call order
- Expecting errors on calling inherited methods
abstract class Shape {
abstract area(): number;
}
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
}Solution
Step 1: Check abstract method implementation
The abstract classShapedeclares an abstract methodarea(). Any subclass must implement this method.Step 2: Verify Circle class methods
TheCircleclass does not implementarea(), so this causes a compile-time error.Final Answer:
Circle must implement the abstract method area() -> Option AQuick Check:
Abstract methods require subclass implementation [OK]
- Forgetting to implement abstract methods
- Thinking super() call is not mandatory when base has no constructor
- Confusing abstract methods with optional methods
abstract class Shape {
abstract area(): number;
}
// Choose the correct Rectangle class implementationSolution
Step 1: Check constructor and super() call
SinceShapehas no constructor, callingsuper()is required before usingthisin the subclass constructor.Step 2: Verify area() method correctness
class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } area(): number { return this.width * this.height; } } correctly implementsarea()returning width * height, which is the rectangle's area.Step 3: Check other options for errors
class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { this.width = width; this.height = height; } area(): number { return this.width + this.height; } } return width + height, which is incorrect for area. class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } } lacksarea()implementation, causing error.Final Answer:
class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } area(): number { return this.width * this.height; } } -> Option BQuick Check:
Correct area method and constructor = class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } area(): number { return this.width * this.height; } } [OK]
- Returning sum instead of product for area
- Omitting abstract method implementation
- Forgetting to assign constructor parameters
Ready to prove it?
Pass the test and this pattern moves unknown
