03 / 03

What are abstract classes?

Difficulty: 4/10
inheritance, polymorphism, type safety

An abstract class is a base class from which other classes can be derived. An abstract class serves as a foundational blueprint for creating more concrete classes.

  1. 1

    Abstract classes cannot be instantiated directly.

  2. 2

    They can implement methods of their own.

  3. 3

    They can define methods that inheriting classes must implement.

  4. 4

    They are primarily used for inheritance, allowing other classes to extend from them.

Key Characteristics:
  1. 1

    Abstract methods or properties: Abstract classes typically include one or more abstract methods or property declarations.

  2. 2

    Inheritance: Other classes derive from abstract classes to share common functionality.

  3. 3

    No direct instantiation: You cannot create an instance of an abstract class directly.

javascript
  1. 1

    A property name.

  2. 2

    A normal method display().

  3. 3

    An abstract method find(name: string): Person.

javascript
  1. 1

    Employee derives from Person.

  2. 2

    Employee must implement the find() method defined in Person.

  3. 3

    The abstract class enforces a contract that derived classes must fulfill.

Notes:
  1. 1

    When implementing an abstract class, the derived class must call super() in its constructor.

  2. 2

    Abstract classes can also include abstract properties.

  3. 3

    Remember, abstract classes provide a powerful way to structure and share functionality while ensuring adherence to a specific contract.

Scenario Questions

0-2 years experience

  1. 1We have a simple `Shape` interface with an `area()` method. How would you refactor it into an abstract class so that `Circle` and `Rectangle` can share a common `color` property and a default `toString()` implementation?
  2. 2If you declare an abstract class `Vehicle` with an abstract `startEngine()` method, what error do you get when a subclass forgets to implement `startEngine`, and how would you fix it?
  3. 3Can you try to instantiate an abstract class directly in TypeScript? What compile‑time error appears, and why?

2-5 years experience

  1. 1Our codebase defines an abstract `BaseApiService` that provides a protected `httpClient`. A new service extending it crashes at runtime with `Cannot read property 'get' of undefined`. Walk me through how you'd debug this issue.
  2. 2We're deciding between an abstract class and an interface for a plugin system that needs shared helper methods. What factors would guide your choice?
  3. 3During a refactor a developer changed a concrete method in an abstract class to abstract, causing compile errors in several subclasses. How would you resolve the breakage while keeping the change minimal?

5-8 years experience

  1. 1Our microservice framework uses an abstract `RequestHandler` with a synchronous `handle()` method. We now need to support async handlers without breaking existing subclasses. How would you evolve the abstract class design?
  2. 2A large UI component hierarchy extends an abstract `Component` class. Adding a new lifecycle hook caused noticeable performance regressions. How would you evaluate the impact and decide whether to keep the hook?
  3. 3When abstract classes are exported from separate npm packages, we sometimes see `instanceof` checks fail because of duplicate type definitions. Explain why this happens and how to prevent it.

8+ years experience

  1. 1Our legacy JavaScript code uses function constructors to mimic inheritance. We're migrating to TypeScript and want to replace them with abstract classes while preserving backward compatibility. Outline a migration plan.
  2. 2We need a contract for all data‑access objects across multiple services. Would you choose an abstract class, an interface, or a hybrid approach? Discuss the long‑term maintenance and versioning implications.
  3. 3A cross‑team initiative is releasing a public SDK that relies on abstract classes for extensibility. What versioning strategy and compatibility guarantees would you put in place to avoid breaking downstream consumers?

Follow-up Questions

  • What trade‑offs does that introduce compared to using an interface?
  • How does TypeScript enforce the abstract contract at compile time?
  • Can you think of any scenario where making a method concrete in the abstract class could cause issues later?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.