When devising the architecture of your classes, a few concerns will pop up:

  • How its instances are going to be used
  • Discover common properties and patterns
  • Foresee how this class is going to be extended in the future

You can leverage your implementations by using abstract base classes and interfaces. Often these two raise a lot of confusion since they are similar, however, they actually have different implementations.

Let’s find out when to use each. For this, we will see examples using different programming languages (Kotlin, TypeScript, Java), but the theory applies to all of them.


What is an abstract class?

A class can be declared abstract when you want common methods or properties to be extended by other classes (or other abstract classes).

When an abstract class defines an abstract method, this method must be overridden by the extending subclass — unless the abstract class is extended by another abstract class, in which case you’ll get a cascading inheritance.

These abstract methods, defined in the abstract class, are created having only their signature. They shouldn’t have a body at this point, that will be defined in the overridden method.

Likewise, you can define concrete (non-abstract) methods that will be inherited by the extending class.

#programming #theory #abstract #object-oriented #interfaces

When to use `Abstract` Base Class vs `Interface`
8.50 GEEK