Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  Components in Classes and Interfaces →  Events 

Events in Inheritance

This example demonstrates the behavior of events in inheritance.

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

A static event ce1 and an instance event ie1 are declared in a superclass c1. The superclass c1, its subclass c2, and its subclass c3 each contain a static method and an instance method for raising the events.

In the class c4, event handlers are declared in c2 for the events inherited from c1. The method main of the class event_demo registers the events and calls all methods that can raise events. Different options are used to address the methods. An "X" is displayed after the method call for a handled event.

  • The event handler cm4 declared for the class c2 can only handle events that are raised in this class or in its subclasses. Events raised by the static method cm1 in c1 are not handled, regardless of the class name specified for the call (see also Inheritance and Static Components).
  • The event handler im4 declared for the class c2 can also only handle events raised 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 raised events in the inherited method im1 of the classes c2 and c3 are handled.