Skip to the solution
MediumFrontend11 min read

What are abstract classes?

820 views

The problem

Question presented to candidate: "What is an abstract class in TypeScript? When would you reach for one instead of an interface, and what does the abstract keyword actually do at run time?"

What a strong answer should cover:

  • An abstract class cannot be instantiated with new; it exists to be extended, and it can mix finished code (fields, constructor, concrete methods) with abstract members that have no body
  • Every non-abstract subclass must implement all abstract members, or tsc reports an error; a subclass can also stay abstract and pass the duty down
  • The template method pattern: a concrete base method calls abstract hooks that each subclass fills in
  • Abstract class vs interface: the class carries shared code and state and survives to run time (instanceof works), an interface is a pure shape with no code and is erased
  • abstract itself is compile-time only: the emitted JavaScript is an ordinary class, so the "cannot instantiate" rule is a type check, not a run-time guard
  • Constructor types: new (...) => T rejects an abstract class, abstract new (...) => T (TypeScript 4.2) accepts it, which is what mixins over abstract bases need

Code / implementation expected: Yes β€” a small abstract base class with one abstract method, two subclasses, and a concrete method that uses the abstract one.

  • abstract

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read Β· 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptAn abstract base class with a template method
TypeScriptAbstract class vs interface, and abstract constructor types
TypeScriptThe constructor-order trap with abstract hooks

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 28 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is declaration merging?

Continue