INTERFACE

From Abap2Java

Jump to: navigation, search

ABAP

As in Java, INTERFACEs in ABAP are used to decouple the behaviour and the implementation of a certain functionality.

INTERFACE if_comparable.
   METHODS compare_to IMPORTING ir_object TYPE REF TO cl_object
                      EXPORTING rv_is_greater TYPE I.
ENDINTERFACE.
CLASS cl_client DEFINITION.
  PUBLIC SECTION:
    INTERFACES if_comparable.
ENDCLASS.

CLASS cl_client IMPLEMENTATION.
  METHOD if_comparable~compare_to IMPORTING ir_object TYPE REF TO cl_object
                                  EXPORTING rv_is_greater TYPE I.
    IF ir_object->some_value > me->some_value.
      rv_is_greater = 1. 
    ELSEIF ir_object->some_value = me->some_value.
      rv_is_greater = 0.
    ELSE.
      rv_is_greater = -1.
    ENDIF.    
  ENDMETHOD.
ENDCLASS.

In this example, the interface if_comparable defines the signature of the method compare_to. Each class that implements the interface has to implement this method. The interface can for example be used as a parameter type of a method.

 METHOD do_the_comparison.
   "IMPORTING PARAMETER ir_comparable_object TYPE REF TO if_comparable.
   result = ir_comparable_object->if_comparable~compare_to( some_other_object ).
 ENDMETHOD. 

Java

see interface

Personal tools