SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

SAP ABAP Objects: Demonstration of inheritance and Events for beginners

SAP ABAP Objects: Demonstration of inheritance and Events for beginners

The concept of inheritance allows you to derive new classes from existing classes. To do this, you use the INHERITING FROM addition of the CLASS ... DEFINITION. The new class adopts or inherits all components of the existing class. The new class is called subclass, and the existing class is called superclass.

 

Events are declared using the statements EVENTS and CLASS-EVENTS, which are possible only in the declaration part of classes and interfaces. Declaring an event in a class means that the methods of the class trigger the event and can therefore cause execution of event handlers. In the declaration, you can define output parameters for an event, for which actual parameters are transferred to the event handler when the event is executed.

 

Below is the example to demonstrate the inheritance and events.


REPORT ZSTP_DEMO_INHERITENCE_EVENTS.

************************************************************************
* Simple example for inheritance and events
************************************************************************

INTERFACE status.
  METHODS write.
ENDINTERFACE.

*-----------------------------------------------------------------------

CLASS vehicle DEFINITION.
  PUBLIC SECTION.
    INTERFACES status.
    METHODS: speed_up IMPORTING step TYPE i,
             stop.
  PROTECTED SECTION.
    DATA: speed     TYPE i,
          max_speed TYPE i VALUE 50.
ENDCLASS.

CLASS vehicle IMPLEMENTATION.
  METHOD speed_up.
    speed = speed + step.
    IF speed > max_speed.
      speed = max_speed.
    ENDIF.
  ENDMETHOD.
  METHOD stop.
    speed = 0.
  ENDMETHOD.
  METHOD status~write.
    WRITE: / 'Vehicle:',
             'Speed =',     speed,
             'Max-Speed =', max_speed.
  ENDMETHOD.
ENDCLASS.

*-----------------------------------------------------------------------

CLASS truck DEFINITION INHERITING FROM vehicle.
  PUBLIC SECTION.
    METHODS: constructor,
             status~write REDEFINITION.
ENDCLASS.

CLASS truck IMPLEMENTATION.
  METHOD constructor.
    call method super->constructor.
    max_speed = 100.
  ENDMETHOD.
  METHOD status~write.
    WRITE: / 'Truck',
             'Speed =',     speed,
             'Max-Speed =', max_speed.
  ENDMETHOD.
ENDCLASS.

*-----------------------------------------------------------------------

CLASS ship DEFINITION INHERITING FROM vehicle.
  PUBLIC SECTION.
    DATA name TYPE string READ-ONLY.
    METHODS: constructor IMPORTING name TYPE string,
             status~write REDEFINITION,
             speed_up REDEFINITION.
    EVENTS damaged.
ENDCLASS.

CLASS ship IMPLEMENTATION.
  METHOD constructor.
    call method super->constructor.
    max_speed = 30.
    me->name = name.
  ENDMETHOD.
  METHOD status~write.
    WRITE: /  name,
             'Speed =',     speed,
             'Max-Speed =', max_speed.
  ENDMETHOD.
  METHOD speed_up.
    speed = speed + step.
      IF speed > max_speed.
        max_speed = 0.
        CALL METHOD stop.
        RAISE EVENT damaged.
      ENDIF.
  ENDMETHOD.
ENDCLASS.

*-----------------------------------------------------------------------

CLASS helicopter DEFINITION.
  PUBLIC SECTION.
    INTERFACES status.
    ALIASES write FOR status~write.
    METHODS RECEIVE FOR EVENT DAMAGED OF ship
                    IMPORTING sender.
ENDCLASS.

CLASS helicopter IMPLEMENTATION.
  METHOD status~write.
    ULINE.
    WRITE: 'Helicopter'.
  ENDMETHOD.
  METHOD RECEIVE.
    CALL METHOD me->write.
    WRITE: 'received call from', sender->name.
    ULINE.
  ENDMETHOD.
ENDCLASS.

************************************************************************
* Class MAIN, enter your coding inside method START
************************************************************************

CLASS main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS start.
ENDCLASS.

CLASS main IMPLEMENTATION.
  METHOD start.
    DATA: status TYPE REF TO status,
          status_tab TYPE TABLE OF REF TO status,
          truck  TYPE REF TO truck,
          ship   TYPE REF TO ship,
          heli   TYPE REF TO helicopter.
    CREATE OBJECT: truck TYPE truck,
                    ship TYPE ship EXPORTING name = 'Titanic',
                    heli TYPE helicopter.
    SET HANDLER HELI->RECEIVE FOR ALL INSTANCES.
    APPEND: truck TO status_tab,
            ship  TO status_tab,
            heli  to status_tab.
    CALL METHOD: truck->speed_up EXPORTING step = 30,
                  ship->speed_up EXPORTING step = 10.
    LOOP AT status_tab INTO status.
      CALL METHOD status->write.
    ENDLOOP.
    DO 5 TIMES.
      CALL METHOD: ship->speed_up EXPORTING step = 10.
    ENDDO.
  ENDMETHOD.
ENDCLASS.

************************************************************************
* START-OF-SELECTION is triggered by the runtime system
************************************************************************

START-OF-SELECTION.
  CALL METHOD main=>start.

************************************************************************

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in