Object Oriented Programming in ABAP - Event with event handler method in different class
- Class C1 contains an event E1, for which the triggering method is T1.
- Event handler method for event E1 is M1, placed in the another class,C2.
- Registration is done at runtime for M1.
- Object is created from class C1 and the triggering method T1 is called, which raises the event and ultimately calls event handler method M1 in class C2.
REPORT zstp_oo_abap.
CLASS c1 DEFINITION.
PUBLIC SECTION.
* Creating event : E1
EVENTS: e1.
* Triggering method : T1
METHODS : t1.
ENDCLASS.
CLASS c2 DEFINITION.
PUBLIC SECTION.
* Creating an event handling method.
METHODS: m1 FOR EVENT e1 OF c1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
* Method : T1 will raise the event
METHOD : t1.
WRITE:/5 'I am T1, going to raise event E1'.
RAISE EVENT e1.
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
* Method : M1 will be called when the event is raised
METHOD : m1.
WRITE:/5 ' I am the event handler method in c2'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: oref1 TYPE REF TO c1,
oref2 TYPE REF TO c2.
CREATE OBJECT: oref1 , oref2 .
* Registering the event handler method
SET HANDLER oref2->m1 FOR oref1 .
* Calling the event which will raise the event.
CALL METHOD oref1->t1.
Output-

For an event in a class, there can be more than one event handler methods in same or different class. However, at runtime only one event handler method will be triggered at a time, based on the registration.
- Class C1 contains an event E1, for which the triggering method is T1 and the event handler methods are :-
- M1 in same class C1.
- M2 in another class C2.
- In the START-OF-SELECTION block, objects are created from class C1 and C2.
- First, registration is made using method M1 of class C1 as event handler method.
- Then, the event E1 is raised, calling method T1. This raises event handler method M1 of class C1.
- After that, the earlier registration is de-activated and new registration is made for method M2 of class C2 as event handler method .
- Event E1 is raised calling method T1. This raises event handler method M2 of class C2.
REPORT zstp_oo_abap.
CLASS c1 DEFINITION.
PUBLIC SECTION.
* Creating event : E1
EVENTS: e1.
* Creating an event handling method.
METHODS: m1 FOR EVENT e1 OF c1.
* Method to raise the event
METHODS : t1.
ENDCLASS.
CLASS c2 DEFINITION.
PUBLIC SECTION.
* Creating an event handling method.
METHODS: m2 FOR EVENT e1 OF c1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
* Method : T1 will raise the event
METHOD : t1.
WRITE:/5 'I am T1, going to raise event E1'.
RAISE EVENT e1.
ENDMETHOD.
* Method : M1 will be called when the event is raised
METHOD : m1.
WRITE:/5 ' I am the event handler method M1 in c1'.
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
* Method : M2 will be called when the event is raised
METHOD : m2.
WRITE:/5 ' I am the event handler method M2 in c2'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: oref1 TYPE REF TO c1,
oref2 TYPE REF TO c2.
CREATE OBJECT: oref1 , oref2 .
* Registering the event handler method
SET HANDLER oref1->m1 FOR oref1 .
* Calling the event which will raise the event.
CALL METHOD oref1->t1.
* De-Registering the earlier event handler method
SET HANDLER oref1->m1 FOR oref1 ACTIVATION space .
* Registering the new event handler method
SET HANDLER oref2->m2 FOR oref1 .
* Calling the event which will raise the event.
CALL METHOD oref1->t1.
Output -
