A class can implement multiple interfaces at once, which allows you to combine functionality from many sources.
Example:
” First interface
INTERFACE if_flyable.
METHODS: fly.
ENDINTERFACE.
” Second interface
INTERFACE if_swimmable.
METHODS: swim.
ENDINTERFACE.
” A class implementing multiple interfaces
CLASS cl_duck DEFINITION.
PUBLIC SECTION.
INTERFACES: if_flyable,
if_swimmable.
METHODS: display.
ENDCLASS.
CLASS cl_duck IMPLEMENTATION.
METHOD if_flyable~fly.
WRITE: / ‘The duck is flying.’.
ENDMETHOD.
METHOD if_swimmable~swim.
WRITE: / ‘The duck is swimming.’.
ENDMETHOD.
METHOD display.
WRITE: / ‘I am a duck with multiple abilities!’.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(duck) = NEW cl_duck( ).
duck->display( ).
duck->if_flyable~fly( ).
duck->if_swimmable~swim( ).
Output:
I am a duck with multiple abilities!
The duck is flying.
The duck is swimming.
Key Points:
-
CLASS … DEFINITION
usesINTERFACES:
to include multiple interfaces. -
Methods from interfaces must be implemented in the class.
-
When calling interface methods, you can:
-
Use fully qualified names:
object->if_interface~method( )
-
Or, if redefined explicitly as public methods, call them directly.
-
-
This is the standard way to achieve “multiple inheritance” in ABAP.