An abstract class is a class that cannot be instantiated directly. It is meant to be a base class for other classes and can contain abstract methods that must be implemented by subclasses.
Click to reveal answer
beginner
Can you create an object from an abstract class?
No, you cannot create an object directly from an abstract class. You must create an object from a subclass that extends the abstract class and implements its abstract methods.
Click to reveal answer
beginner
What is an abstract method?
An abstract method is a method declared in an abstract class without an implementation. Subclasses must provide their own implementation of this method.
Click to reveal answer
beginner
How do you declare an abstract class and an abstract method in TypeScript?
Use the keyword <code>abstract</code> before the class name to declare an abstract class. Use <code>abstract</code> before a method signature inside the class to declare an abstract method.
Click to reveal answer
intermediate
Why use abstract classes instead of interfaces in TypeScript?
Abstract classes can have both implemented methods and abstract methods, allowing shared code and enforced method implementation. Interfaces only declare method signatures without implementation.
Click to reveal answer
Which keyword is used to declare an abstract class in TypeScript?
Aabstract
Binterface
Cclass
Dimplements
✗ Incorrect
The keyword abstract is used before the class name to declare an abstract class.
Can you instantiate an abstract class directly?
AYes, always
BNo, never
COnly if it has no abstract methods
DOnly inside subclasses
✗ Incorrect
Abstract classes cannot be instantiated directly; you must instantiate a subclass.
What must a subclass do if it extends an abstract class with abstract methods?
AOverride only some abstract methods
BIgnore abstract methods
CImplement all abstract methods
DConvert abstract methods to properties
✗ Incorrect
Subclasses must implement all abstract methods declared in the abstract class.
Which of these can an abstract class contain?
ABoth abstract and implemented methods
BOnly implemented methods
COnly properties
DOnly abstract methods
✗ Incorrect
Abstract classes can have both abstract methods (without body) and implemented methods (with body).
What happens if a subclass does not implement all abstract methods?
AIt automatically implements methods
BIt compiles without errors
CIt throws a runtime error
DIt becomes an abstract class itself
✗ Incorrect
If a subclass does not implement all abstract methods, it must be declared abstract itself.
Explain what an abstract class is and why you would use it in TypeScript.
Think about a blueprint that cannot be built but guides other buildings.
You got /4 concepts.
Describe the difference between an abstract class and an interface in TypeScript.
One can have code inside, the other only rules.
You got /4 concepts.
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
Step 1: Understand abstract class instantiation rules
Abstract classes cannot be instantiated directly, so you cannot create objects from them using new.
Step 2: Compare other options
Abstract classes can have implemented methods, are different from interfaces, and cannot be instantiated with new.
Final Answer:
You cannot create an instance of an abstract class directly. -> Option C
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
Step 1: Recall TypeScript abstract class syntax
The correct syntax places the keyword abstract before class, followed by the class name.
Step 2: Check each option
Only abstract class Vehicle {} correctly uses abstract class Vehicle {}. Others have incorrect keyword order.
Final Answer:
abstract class Vehicle {} -> Option A
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
Step 1: Understand class inheritance and method calls
The class Dog extends Animal and implements makeSound(). Calling dog.makeSound() prints 'Bark'.
Step 2: Check the move() method call
The move() method is implemented in Animal and prints 'Moving'. Calling dog.move() prints 'Moving'.
Final Answer:
Bark
Moving -> Option D
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
Step 1: Check abstract method implementation
The abstract class Shape declares an abstract method area(). Any subclass must implement this method.
Step 2: Verify Circle class methods
The Circle class does not implement area(), so this causes a compile-time error.
Final Answer:
Circle must implement the abstract method area() -> Option A