Sobes.tech
Junior — Middle

In what cases is it recommended to use abstract classes in object-oriented programming?

sobes.tech AI

Answer from AI

Abstract classes are used when you need to define a common template for a group of related classes, but you cannot or do not want to create instances of the abstract class itself. They allow you to define basic functionality and an interface that inheritors must implement.

It is recommended to use abstract classes if:

  • There is common logic that can be implemented once in the base class.
  • You need to ensure that certain methods are implemented in subclasses (via abstract methods).
  • Partial implementation is required, which inheritors can extend or override.

For example, in Android, you can create an abstract class BaseActivity that contains common methods for all activities, while specific screens implement their own logic.