Program to the capability; let the object do its job
An interface reference can point to different objects that implement the same contract. The caller sends the same method call; ABAP dispatches it to the implementation belonging to the actual object. This is polymorphism.
Same call, variable implementation
DATA(notifier) TYPE REF TO lif_notifier.
notifier = NEW lcl_log_notifier( ).
DATA(result) = notifier->send( iv_message = 'Parcel ready' ).
The variable is typed to lif_notifier, not to lcl_log_notifier. Later, a different implementation can be assigned without changing the consumer, as long as it fulfills that interface.
What this buys you
- The workflow is not filled with
IF channel = ...branches. - Each implementation owns its delivery detail.
- Tests can provide a fake notifier and check the consumer’s behavior.
Polymorphism does not require an interface in every case; an eligible superclass reference can also refer to subclass objects. Interface-based polymorphism is often a clean fit for roles/capabilities.
Practice
Declare an interface reference, assign an implementing class instance, call send, and set result. Add WRITE result. in a classic report or the Cloud class-run output call. This exercise checks patterns and does not execute them.
Version note: Dynamic method dispatch is an ABAP Objects concept; the concrete APIs and language-version restrictions remain environment-dependent.
Sources
- SAP Samples, ABAP Object Orientation: polymorphism and interfaces
- SAP Help Portal, ABAP Objects overview
Developer practice · ABAP
Practice: call different notifier implementations
Type a reference to lif_notifier, assign an lcl_log_notifier instance, call send through the interface, and set result.
This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.
Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.
0 required structures detected · 0 checks
Write your solution, then check its structure.
One possible solution
DATA(notifier) TYPE REF TO lif_notifier.
notifier = NEW lcl_log_notifier( ).
DATA(result) = notifier->send( iv_message = 'Parcel ready' ).
WRITE result.out->write( result ).These are context-specific output examples. In your own program, use the result variable from your solution and the output mechanism available in that program.