What are Abstract Classes in TypeScript?
Learn what TypeScript abstract classes are, how they differ from interfaces, how to implement abstract members, and when to use them in OOP design.
Expected Interview Answer
An abstract class in TypeScript is a base class marked with the abstract keyword that cannot be instantiated directly and can define abstract members that subclasses must implement.
Abstract classes sit between interfaces and concrete classes: like interfaces they declare a contract via abstract methods and properties, but unlike interfaces they can also provide shared implementation, state, and constructors. Subclasses extend the abstract class and must implement every abstract member before they can be instantiated, making abstract classes ideal for template-method patterns and shared base behaviour.
- Enforces a contract that subclasses must implement
- Shares common implementation and state across subclasses
- Prevents accidental instantiation of an incomplete base type
- Supports the template-method pattern with concrete plus abstract members
- Combines a contract with reusable code, unlike a pure interface
AI Mentor Explanation
Think of the general idea of a bowler: every bowler must have a deliver() action, but no one walks onto the pitch as an abstract 'bowler' — they are a fast bowler or a spinner. An abstract class is that base concept; it demands the deliver() method exists and shares common run-up logic, but only concrete bowler types can actually take the field.
Step-by-Step Explanation
Step 1
Declare the abstract class
Mark the class with the abstract keyword so it cannot be instantiated directly.
Step 2
Add abstract members
Declare abstract methods or properties with no body that subclasses must implement.
Step 3
Provide shared code
Add concrete methods, fields, and a constructor for behaviour common to all subclasses.
Step 4
Extend it
Create subclasses with 'extends' that implement every abstract member.
Step 5
Instantiate the subclass
Create instances of the concrete subclass, never of the abstract base itself.
What Interviewer Expects
- Knowing abstract classes cannot be instantiated directly
- Difference between abstract classes and interfaces
- That abstract members must be implemented by subclasses
- Ability to mix abstract and concrete members
- A real example using the abstract and extends keywords
Common Mistakes
- Trying to instantiate the abstract class with 'new'
- Forgetting to implement an abstract member in a subclass
- Confusing abstract classes with interfaces (interfaces hold no implementation)
- Giving an abstract method a body
- Assuming multiple inheritance works — a class extends only one base
Best Answer (HR Friendly)
“An abstract class is a base class you cannot create directly; it defines rules that its child classes must follow while also sharing some ready-made behaviour. It is useful when several related classes share logic but each must fill in specific details.”
Code Example
abstract class Shape {
constructor(protected name: string) {}
// must be implemented by subclasses
abstract area(): number;
// shared concrete method
describe(): string {
return `${this.name} has area ${this.area().toFixed(2)}`;
}
}
class Circle extends Shape {
constructor(private radius: number) {
super('Circle');
}
area(): number {
return Math.PI * this.radius ** 2;
}
}
const c = new Circle(2);
console.log(c.describe());
// const s = new Shape('x'); // Error: cannot create an instance of an abstract classFollow-up Questions
- How does an abstract class differ from an interface?
- Can an abstract class have a constructor and state?
- What happens if a subclass does not implement an abstract member?
- Can you have abstract properties as well as abstract methods?
- How does the template-method pattern use abstract classes?
MCQ Practice
1. What happens if you try to instantiate an abstract class directly?
Abstract classes cannot be instantiated; attempting 'new' on one is a compile-time error.
2. A key difference between an abstract class and an interface is that an abstract class...
Unlike interfaces, abstract classes can carry concrete method bodies, fields, and constructors.
3. What must a subclass do with an abstract method?
Subclasses must implement every abstract member before they can be instantiated.
Flash Cards
Can you instantiate an abstract class? — No — it can only be extended; instances are created from concrete subclasses.
Abstract class vs interface? — Abstract classes can include implementation, state, and constructors; interfaces only declare shape.
What must subclasses do with abstract members? — Implement every abstract method and property before they can be instantiated.
Keyword to declare one? — The 'abstract' keyword on the class and on its unimplemented members.