Java / MapStruct Java Interview questions part 2
When would you choose an abstract class over an interface for a mapper?
An interface is enough when the mapper only needs pure, stateless conversions that MapStruct can fully generate. You reach for an abstract class when the mapper needs any of the following, none of which a plain interface supports:
- Injected fields or constructor-based dependency injection.
- Private or protected helper methods that generated code should call but that shouldn't be part of the mapper's public contract.
- Shared state or initialization logic across multiple mapping methods.
MapStruct still generates the concrete subclass and fills in the abstract methods exactly as it would for an interface; the abstract class simply gives you a place to put ordinary Java members alongside the generated code.
More Related questions...