Abstract classes help you create a base blueprint for other classes. They let you define common features while forcing specific parts to be made by child classes.
Abstract classes in Typescript
abstract class Animal { abstract makeSound(): void; move(): void { console.log('Moving around'); } } class Dog extends Animal { makeSound(): void { console.log('Bark'); } }
An abstract class cannot be instantiated directly.
Abstract methods have no body and must be implemented by subclasses.
startEngine must be defined in Car.abstract class Vehicle { abstract startEngine(): void; } class Car extends Vehicle { startEngine(): void { console.log('Car engine started'); } }
Shape forces area method. Circle provides its own calculation.abstract class Shape { abstract area(): number; } class Circle extends Shape { radius: number; constructor(radius: number) { super(); this.radius = radius; } area(): number { return Math.PI * this.radius * this.radius; } }
abstract class Employee { abstract getSalary(): number; } // Error: Cannot create instance of abstract class // const emp = new Employee();
This program shows an abstract class Animal with an abstract method makeSound. Two classes Dog and Cat implement this method. We create objects of Dog and Cat and call their methods.
abstract class Animal { abstract makeSound(): void; move(): void { console.log('Moving around'); } } class Dog extends Animal { makeSound(): void { console.log('Bark'); } } class Cat extends Animal { makeSound(): void { console.log('Meow'); } } const dog = new Dog(); dog.move(); dog.makeSound(); const cat = new Cat(); cat.move(); cat.makeSound();
Time complexity depends on the methods you implement, not on abstract classes themselves.
Abstract classes help organize code and enforce rules for subclasses.
Common mistake: Trying to create an instance of an abstract class directly causes an error.
Use abstract classes when you want to share code and force certain methods to be implemented. Use interfaces if you only want to enforce method signatures without code.
Abstract classes provide a base with some shared code and some methods to be defined by subclasses.
You cannot create objects from abstract classes directly.
They help organize related classes and enforce important methods.
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
