Bird
Raised Fist0
Typescriptprogramming~20 mins

Abstract classes in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Abstract Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of abstract class method call

What is the output of this TypeScript code?

Typescript
abstract class Animal {
  abstract makeSound(): string;
  move(): string {
    return "Moving";
  }
}

class Dog extends Animal {
  makeSound(): string {
    return "Bark";
  }
}

const dog = new Dog();
console.log(dog.makeSound());
console.log(dog.move());
ABark\nundefined
BBark\nMoving
CError: Cannot create an instance of an abstract class
DMoving\nBark
Attempts:
2 left
💡 Hint

Remember that abstract classes cannot be instantiated directly, but their subclasses can be.

🧠 Conceptual
intermediate
1:30remaining
Purpose of abstract classes

What is the main purpose of using an abstract class in TypeScript?

ATo create objects directly without subclassing
BTo allow multiple inheritance of classes
CTo prevent any class from inheriting from it
DTo provide a base class with some implemented methods and some methods that must be implemented by subclasses
Attempts:
2 left
💡 Hint

Think about how abstract classes help organize code and enforce method implementation.

🔧 Debug
advanced
1:30remaining
Identify the error in abstract class usage

What error will this TypeScript code produce?

Typescript
abstract class Vehicle {
  abstract startEngine(): void;
}

const v = new Vehicle();
AError: Cannot create an instance of an abstract class
BError: Abstract method must have an implementation
CNo error, instance created successfully
DError: Missing constructor in abstract class
Attempts:
2 left
💡 Hint

Can you create an object directly from an abstract class?

📝 Syntax
advanced
1:30remaining
Correct syntax for abstract method

Which option shows the correct syntax for declaring an abstract method in a TypeScript abstract class?

Typescript
abstract class Shape {
  // Choose the correct abstract method declaration
}
Aabstract calculateArea(): number;
Babstract calculateArea() { return 0; }
CcalculateArea(): abstract number;
Dabstract calculateArea: () => number;
Attempts:
2 left
💡 Hint

Abstract methods do not have a body and end with a semicolon.

🚀 Application
expert
2:00remaining
Number of methods implemented in subclass

Given this abstract class and subclass, how many methods does the Circle class implement?

Typescript
abstract class Shape {
  abstract area(): number;
  abstract perimeter(): number;
  description(): string {
    return "A shape";
  }
}

class Circle extends Shape {
  radius: number;
  constructor(radius: number) {
    super();
    this.radius = radius;
  }
  area(): number {
    return Math.PI * this.radius ** 2;
  }
  perimeter(): number {
    return 2 * Math.PI * this.radius;
  }
}
A0
B3
C2
D1
Attempts:
2 left
💡 Hint

Count only the methods that must be implemented by the subclass.

Practice

(1/5)
1. What is true about an abstract class in TypeScript?
easy
A. Abstract classes are the same as interfaces.
B. Abstract classes cannot have any methods implemented.
C. You cannot create an instance of an abstract class directly.
D. You must use the new keyword to instantiate an abstract class.

Solution

  1. Step 1: Understand abstract class instantiation rules

    Abstract classes cannot be instantiated directly, so you cannot create objects from them using new.
  2. Step 2: Compare other options

    Abstract classes can have implemented methods, are different from interfaces, and cannot be instantiated with new.
  3. Final Answer:

    You cannot create an instance of an abstract class directly. -> Option C
  4. Quick Check:

    Abstract class instantiation = not allowed [OK]
Hint: Remember: abstract classes can't be directly instantiated [OK]
Common Mistakes:
  • Thinking abstract classes are interfaces
  • Assuming abstract classes can't have implemented methods
  • Trying to instantiate abstract classes directly
2. Which of the following is the correct syntax to declare an abstract class in TypeScript?
easy
A. abstract class Vehicle {}
B. class abstract Vehicle {}
C. abstract Vehicle class {}
D. class Vehicle abstract {}

Solution

  1. Step 1: Recall TypeScript abstract class syntax

    The correct syntax places the keyword abstract before class, followed by the class name.
  2. Step 2: Check each option

    Only abstract class Vehicle {} correctly uses abstract class Vehicle {}. Others have incorrect keyword order.
  3. Final Answer:

    abstract class Vehicle {} -> Option A
  4. Quick Check:

    Abstract class syntax = abstract class Name {} [OK]
Hint: Put 'abstract' before 'class' keyword [OK]
Common Mistakes:
  • Placing 'abstract' after 'class'
  • Mixing keyword order
  • Omitting 'abstract' keyword
3. What will be the output of the following TypeScript code?
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();
medium
A. Moving\nBark
B. No output
C. Error: Cannot create instance of abstract class
D. Bark\nMoving

Solution

  1. Step 1: Understand class inheritance and method calls

    The class Dog extends Animal and implements makeSound(). Calling dog.makeSound() prints 'Bark'.
  2. Step 2: Check the move() method call

    The move() method is implemented in Animal and prints 'Moving'. Calling dog.move() prints 'Moving'.
  3. Final Answer:

    Bark Moving -> Option D
  4. Quick Check:

    Subclass methods run, abstract class methods inherited [OK]
Hint: Abstract methods must be implemented; inherited methods run normally [OK]
Common Mistakes:
  • Thinking abstract class instance is created
  • Confusing method call order
  • Expecting errors on calling inherited methods
4. Identify the error in the following TypeScript code:
abstract class Shape {
  abstract area(): number;
}

class Circle extends Shape {
  radius: number;
  constructor(radius: number) {
    super();
    this.radius = radius;
  }
}
medium
A. Circle must implement the abstract method area()
B. Abstract class cannot have abstract methods
C. radius property should be private
D. Missing call to super() in Circle constructor

Solution

  1. Step 1: Check abstract method implementation

    The abstract class Shape declares an abstract method area(). Any subclass must implement this method.
  2. Step 2: Verify Circle class methods

    The Circle class does not implement area(), so this causes a compile-time error.
  3. Final Answer:

    Circle must implement the abstract method area() -> Option A
  4. Quick Check:

    Abstract methods require subclass implementation [OK]
Hint: All abstract methods must be implemented in subclasses [OK]
Common Mistakes:
  • Forgetting to implement abstract methods
  • Thinking super() call is not mandatory when base has no constructor
  • Confusing abstract methods with optional methods
5. Given the abstract class below, which implementation correctly uses it to calculate the area of a rectangle?
abstract class Shape {
  abstract area(): number;
}

// Choose the correct Rectangle class implementation
hard
A. 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; } }
B. 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; } }
C. 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; } }
D. class Rectangle extends Shape { width: number; height: number; constructor(width: number, height: number) { super(); this.width = width; this.height = height; } }

Solution

  1. Step 1: Check constructor and super() call

    Since Shape has no constructor, calling super() is required before using this in the subclass constructor.
  2. 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 implements area() returning width * height, which is the rectangle's area.
  3. 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; } } lacks area() implementation, causing error.
  4. 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 B
  5. Quick 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]
Hint: Area of rectangle = width * height; implement abstract method [OK]
Common Mistakes:
  • Returning sum instead of product for area
  • Omitting abstract method implementation
  • Forgetting to assign constructor parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard