Start of Content Area

Interfaces - Introductory Example  Locate the document in its SAP Library structure

The following simple example shows how you can use an interface called status to output the attributes of an object and illustrates how this interface is implemented in two different classes.

Example

REPORT demo_interface.

INTERFACE status.
  METHODS write.
ENDINTERFACE.

CLASS counter DEFINITION.
  PUBLIC SECTION.
    INTERFACES status.
    METHODS increment.
  PRIVATE SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD status~write.
    WRITE: / 'Count in counter is', count.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
ENDCLASS.

CLASS bicycle DEFINITION.
  PUBLIC SECTION.
    INTERFACES status.
    METHODS drive.
  PRIVATE SECTION.
    DATA speed TYPE i.
ENDCLASS.

CLASS bicycle IMPLEMENTATION.
  METHOD status~write.
    WRITE: / 'Speed of bicycle is', speed.
  ENDMETHOD.
  METHOD drive.
    ADD 10 TO speed.
  ENDMETHOD.
ENDCLASS.

The interface status contains one method called write. The classes counter and bicyleimplement the interface in the public section. Both classes must implement the interface method in their implementation part in accordance with the semantics required.

The following sections explain how a user can use interface references to address the objects of both classes:

This graphic is explained in the accompanying text

 

 

 

End of Content Area