ABAP Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Classes and Interfaces → Components in Classes and Interfaces → Events
Inheritance Events
The example demonstrates the behavior of inheritance events.
Other versions: 7.31 | 7.40 | 7.54
Source Code
REPORT demo_event_inheritance.
CLASS c1 DEFINITION.
PUBLIC SECTION.
CLASS-EVENTS ce1.
CLASS-METHODS cm1.
EVENTS ie1.
METHODS im1.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
CLASS-METHODS cm2.
METHODS im2.
ENDCLASS.
CLASS c3 DEFINITION INHERITING FROM c2.
PUBLIC SECTION.
CLASS-METHODS cm3.
METHODS im3.
ENDCLASS.
CLASS c4 DEFINITION.
PUBLIC SECTION.
CLASS-METHODS cm4 FOR EVENT ce1 OF c2.
METHODS im4 FOR EVENT ie1 OF c2.
ENDCLASS.
CLASS event_demo DEFINITION.
PUBLIC SECTION.
CLASS-DATA handle_flag TYPE c LENGTH 1.
CLASS-METHODS main.
ENDCLASS.
CLASS event_demo IMPLEMENTATION.
METHOD main.
DATA oref1 TYPE REF TO c1.
DATA oref2 TYPE REF TO c2.
DATA oref3 TYPE REF TO c3.
DATA oref4 TYPE REF TO c4.
DATA(out) = cl_demo_output=>new(
)->begin_section( 'Static event' ).
SET HANDLER c4=>cm4.
c1=>cm1( ).
out->write( |c1=>cm1( ): { handle_flag }| ).
c2=>cm1( ).
out->write( |c2=>cm1( ): { handle_flag }| ).
c3=>cm1( ).
out->write( |c3=>cm1( ): { handle_flag }| ).
c2=>cm2( ).
out->write( |c2=>cm2( ): { handle_flag }| ).
c3=>cm2( ).
out->write( |c3=>cm2( ): { handle_flag }| ).
c3=>cm3( ).
out->write( |c3=>cm3( ): { handle_flag }| ).
out->next_section( 'Instance event' ).
CREATE OBJECT: oref1, oref2, oref3, oref4.
SET HANDLER oref4->im4 FOR ALL INSTANCES.
oref1->im1( ).
out->write( |oref1->im1( ): { handle_flag }| ).
oref2->im1( ).
out->write( |oref2->im1( ): { handle_flag }| ).
oref3->im1( ).
out->write( |oref3->im1( ): { handle_flag }| ).
oref2->im2( ).
out->write( |oref2->im2( ): { handle_flag }| ).
oref3->im2( ).
out->write( |oref3->im2( ): { handle_flag }| ).
oref3->im3( ).
out->write( |oref3->im3( ): { handle_flag }| ).
out->display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
event_demo=>main( ).
CLASS c1 IMPLEMENTATION.
METHOD cm1.
CLEAR event_demo=>handle_flag.
RAISE EVENT ce1.
ENDMETHOD.
METHOD im1.
CLEAR event_demo=>handle_flag.
RAISE EVENT ie1.
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD cm2.
CLEAR event_demo=>handle_flag.
RAISE EVENT ce1.
ENDMETHOD.
METHOD im2.
CLEAR event_demo=>handle_flag.
RAISE EVENT ie1.
ENDMETHOD.
ENDCLASS.
CLASS c3 IMPLEMENTATION.
METHOD cm3.
CLEAR event_demo=>handle_flag.
RAISE EVENT ce1.
ENDMETHOD.
METHOD im3.
CLEAR event_demo=>handle_flag.
RAISE EVENT ie1.
ENDMETHOD.
ENDCLASS.
CLASS c4 IMPLEMENTATION.
METHOD cm4.
event_demo=>handle_flag = 'X'.
ENDMETHOD.
METHOD im4.
event_demo=>handle_flag = 'X'.
ENDMETHOD.
ENDCLASS.
Description
In a c1
superclass, a static event ce1
and an
instance event ie1
are declared. The superclass c1
,
and its subclass c2
, and its subclass c3
, each contain a static method and an instance method to trigger events.
In the c4
class, event handlers are declared in c2
for the events inherited from c1
. The main
method
of the event_demo
class registers events and calls all methods which can
trigger events, whereby different possibilities are used to address the methods. The output displays an "X" after the method call for a handled event.
- The event handler
cm4
, declared for thec2
class, can only handle events which are triggered in this class or in its subclasses. Events which are triggered by the static methodcm1
inc1
, are not handled, irrelevant of the class name which is specified for the call (also refer to Inheritance and Static Components).
- The event handler
im4
, declared for thec2
class, can also only handle events which are triggered in this class or in its subclasses. Unlike when calling static methods, the class of the object is always addressed when addressing an object using an object reference, and triggered events in the inherited methodim1
of thec2
andc3
classes are handled.