01 / 13

What is an Abstract Class? When is it used?

Difficulty: 2/10

Abstract Class

An abstract class is a class intended to serve as a base abstraction and cannot normally be instantiated directly. It can contain both implemented methods and abstract methods that derived classes must implement. I use an abstract class when related types share common state, invariants, or implementation, while still requiring subclasses to provide specific behavior. It is particularly useful when there is a genuine IS-A relationship and some common behavior belongs in the base class.

javascript
  1. 1

    Cannot normally be instantiated directly.

  2. 2

    Can contain abstract and concrete methods.

  3. 3

    Can contain instance state and constructors.

  4. 4

    Useful for sharing common implementation among closely related types.

  5. 5

    Should represent a meaningful abstraction rather than being used merely for code reuse.

Follow-up Questions

  • Can an abstract class have a constructor?
  • Can an abstract class contain concrete methods?
  • Can an abstract class implement an interface?
  • What is the difference between an abstract class and an interface?
Share

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