Classes, Interfaces and Polymorphism
Composition over inheritance, interface segregation in ABAP.
Explanation
Prefer composition โ a class holding an interface reference โ over deep inheritance. Interfaces (IF_โฆ) are the ABAP contract mechanism and let you inject test doubles. Polymorphism lets a single caller work with any implementation of the interface, enabling strategies like payment provider selection.
Code example
INTERFACE if_payment. METHODS charge IMPORTING iv_amount TYPE p RAISING zcx_payment.ENDINTERFACE. CLASS lcl_stripe DEFINITION. PUBLIC SECTION. INTERFACES if_payment.ENDCLASS.Real project scenario
A billing engine used inheritance for every provider variant. Switching to an interface + factory removed 12 subclasses and simplified testing.
Common mistakes
Deep inheritance for reuse; leaking implementation types through public methods; missing FINAL/ABSTRACT modifiers.
Best practices
Program to interfaces; keep classes FINAL by default; expose only interface types from factories.
Interview angle
When is inheritance the right choice over composition?