ABAP Keyword Documentation → ABAP - Reference → Declarative statemnts → Classes and Interfaces → ABAP Objects - Overview → Examples for ABAP Objects
ABAP Objects, Interfaces
This example demonstrates the use of interfaces.
Other versions: 7.31 | 7.40 | 7.54
Source Code
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.
DATA: count TYPE REF TO counter,
bike TYPE REF TO bicycle,
status TYPE REF TO status,
status_tab TYPE TABLE OF REF TO status.
START-OF-SELECTION.
CREATE OBJECT: count, bike.
DO 5 TIMES.
count->increment( ).
bike->drive( ).
ENDDO.
APPEND: count TO status_tab,
bike TO status_tab.
LOOP AT status_tab INTO status.
status->write( ).
ENDLOOP.
Description
This example shows a status
interface for outputting the attributes of an object and its implementation in two different classes.
The status
interface contains a write
method.
The counter
and bicycle
classes implement the
interface in the public area. Both classes must implement the interface method in the implementation part corresponding to the required semantics.
First, two class reference variables are declared, count
and bike
,
for the classes counter
and bicycle
. An interface
reference variable status
and an internal table status_tab
with a suitable row type for the interface reference variable, are created for the status
interface. All the reference variables begin with initial values.
Using the CREATE OBJECT
statement, an object is created for each class, to
which the references in count
and bike
point.
Using the class reference variable, the increment
and drive
methods are called in the respective objects.
Through the addition of the class reference variables to the interface reference table, the rows in
status_tab
also point to the two objects. Using the interface references,
it is possible to call the interface method write
in the objects, but not the class methods increment
or drive
.