ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview → Examples for ABAP Objects
ABAP Objects, Overview
This example demonstrates objects, object references, inheritance, interfaces, and events.
Other versions: 7.31 | 7.40 | 7.54
Source Code
REPORT demo_abap_objects.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS main DEFINITION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Main class definition *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS main DEFINITION.
PUBLIC SECTION.
CLASS-DATA out TYPE REF TO if_demo_output.
CLASS-METHODS:
class_constructor,
start,
objects,
inheritance,
interfaces,
events.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* INTERFACE status</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Interface definition *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
INTERFACE status.
METHODS write.
ENDINTERFACE.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS vessel DEFINITION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Superclass definition *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS vessel DEFINITION.
PUBLIC SECTION.
METHODS:
constructor,
drive
IMPORTING speed_up TYPE i,
get_id
RETURNING VALUE(id) TYPE i.
PROTECTED SECTION.
DATA: speed TYPE i,
max_speed TYPE i VALUE 100.
PRIVATE SECTION.
CLASS-DATA object_count TYPE i.
DATA id TYPE i.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS vessel IMPLEMENTATION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Superclass implementation *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS vessel IMPLEMENTATION.
METHOD constructor.
object_count += 1.
id = object_count.
ENDMETHOD.
METHOD drive.
speed += speed_up.
IF speed > max_speed.
speed = max_speed.
ENDIF.
ENDMETHOD.
METHOD get_id.
id = me->id.
ENDMETHOD.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS ship DEFINITION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Subclass definition *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS ship DEFINITION INHERITING FROM vessel.
PUBLIC SECTION.
INTERFACES status.
DATA name TYPE string READ-ONLY.
METHODS:
constructor
IMPORTING name TYPE string,
drive
REDEFINITION.
EVENTS emergency_call.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS ship IMPLEMENTATION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Subclass implementation *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS ship IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
max_speed = 30.
me->name = name.
ENDMETHOD.
METHOD status~write.
DATA id TYPE c LENGTH 1.
id = me->get_id( ).
main=>out->write( |{ name } is vessel { id
} and has speed { speed }| ).
ENDMETHOD.
METHOD drive.
speed += speed_up.
IF speed > max_speed.
max_speed = 0.
speed = 0.
RAISE EVENT emergency_call.
ENDIF.
ENDMETHOD.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS coast_guard DEFINITION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Event handler definition *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS coast_guard DEFINITION.
PUBLIC SECTION.
INTERFACES status.
METHODS receive
FOR EVENT emergency_call OF ship
IMPORTING sender.
ALIASES write FOR status~write.
PRIVATE SECTION.
DATA caller TYPE string.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS coast_guard IMPLEMENTATION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Event handler implementation *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS coast_guard IMPLEMENTATION.
METHOD status~write.
IF caller IS INITIAL.
main=>out->write( `Coast guard received no call` ).
ELSE.
main=>out->write( |Coast guard received a call from { caller }| ).
ENDIF.
ENDMETHOD.
METHOD receive.
caller = sender->name.
write( ).
ENDMETHOD.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* System event START-OF-SELECTION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Triggered by the ABAP runtime environment automatically *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
START-OF-SELECTION.
main=>start( ).
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* CLASS main IMPLEMENTATION</span>
<span class="blue">*---------------------------------------------------------------------*</span>
<span class="blue">* Main class implementation *</span>
<span class="blue">*---------------------------------------------------------------------*</span>
CLASS main IMPLEMENTATION.
METHOD class_constructor.
out = cl_demo_output=>new( ).
ENDMETHOD.
METHOD start.
DATA: button1 TYPE abap_bool VALUE abap_true,
button2 TYPE abap_bool,
button3 TYPE abap_bool,
button4 TYPE abap_bool.
cl_demo_input=>new(
)->add_field( EXPORTING text = `Objects and Object References`
as_checkbox = abap_true
CHANGING field = button1
)->add_field( EXPORTING text = `Inheritance`
as_checkbox = abap_true
CHANGING field = button2
)->add_field( EXPORTING text = `Interfaces`
as_checkbox = abap_true
CHANGING field = button3
)->add_field( EXPORTING text = `Events`
as_checkbox = abap_true
CHANGING field = button4
)->request( ).
IF strlen( button1 && button2 && button3 && button4 ) > 1.
EXIT.
ELSEIF button1 = abap_true.
objects( ).
ELSEIF button2 = abap_true.
inheritance( ).
ELSEIF button3 = abap_true.
interfaces( ).
ELSEIF button4 = abap_true.
events( ).
ENDIF.
out->display( ).
ENDMETHOD.
METHOD objects.
DATA(vessel1) = NEW vessel( ).
DATA(vessel2) = NEW vessel( ).
vessel1->drive( 50 ).
vessel2->drive( 80 ).
out->write( |Vessel ID is { vessel1->get_id( ) }| ).
out->write( |Vessel ID is { vessel2->get_id( ) }| ).
ENDMETHOD.
METHOD inheritance.
DATA(ship) = NEW ship( 'Titanic' ).
ship->drive( 20 ).
DATA(vessel) = CAST vessel( ship ).
vessel->drive( 10 ).
ship->status~write( ).
ENDMETHOD.
METHOD interfaces.
DATA status_tab TYPE TABLE OF REF TO status WITH EMPTY KEY.
status_tab = VALUE #( ( NEW ship( name = 'Titanic' ) )
( NEW coast_guard( ) ) ).
LOOP AT status_tab ASSIGNING FIELD-SYMBOL(<status>).
<status>->write( ).
ENDLOOP.
ENDMETHOD.
METHOD events.
DATA(ship) = NEW ship( 'Titanic' ).
DATA(station) = NEW coast_guard( ).
SET HANDLER station->receive FOR ship.
DO 5 TIMES.
ship->drive( 10 ).
ENDDO.
ENDMETHOD.
ENDCLASS.
<span class="blue">*---------------------------------------------------------------------*</span>
Description
The program contains a class main
with a method start
which is called at program start and demonstrates the use of ABAP objects using the example of classes of water vehicles.