An interface in ABAP defines a contract that any implementing class must follow. It contains only declarations of:
- Methods (without implementation)
- Constants
- Types
- Attributes
Interfaces do not contain implementation. Any class that implements an interface must provide the logic for all its methods.
Feature | Description |
---|---|
No implementation | Only method signatures are defined. |
Multiple interfaces | A class can implement more than one interface. |
Full implementation required | All interface methods must be implemented in the class. |
Promotes loose coupling | Ideal for modular, testable, flexible code. |
Code:
REPORT zrep_lclclass.
INTERFACE if_animal.
METHODS:
speak,
move.
ENDINTERFACE.
" Class that implements the interface
CLASS lcl_dog DEFINITION.
PUBLIC SECTION.
INTERFACES: if_animal.
ENDCLASS.
CLASS lcl_dog IMPLEMENTATION.
METHOD if_animal~speak.
WRITE: 'Woof!'.
ENDMETHOD.
METHOD if_animal~move.
WRITE: 'Dog is running'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_dog) = NEW lcl_dog( ).
lo_dog->if_animal~speak( ).
lo_dog->if_animal~move( ).
Output: