Sobes.tech
Senior

What was preferred on the project at [company] — an abstract class or an interface?

sobes.tech AI

Answer from AI

In Java, it was usually preferred in the project to use interfaces instead of abstract classes when defining a contract without implementation. Interfaces offer greater flexibility because a class can implement multiple interfaces, whereas inheritance from an abstract class is limited to one parent.

Abstract classes were chosen when partial implementation or common fields for subclasses needed to be provided.

For example, if we have several types of payments, we can create a Payment interface with a pay() method, and an AbstractPayment abstract class to implement common methods and fields, such as amount and date.

Thus, the choice depends on the task: an interface for the contract, an abstract class for partial implementation and shared state.

What was preferred on the project at [company] — an… - sobes.tech