Start of Content Area

Methods in ABAP Objects - Example  Locate the document in its SAP Library structure

The following example shows how to declare, implement, and use methods in ABAP Objects.

Overview

This example uses three classes called C_TEAM, C_BIKER, and C_BICYCLE. A user (a program) can create objects of the class C_TEAM. On a selection screen, the class C_TEAM asks for the number of members of each team.

Each object in the class C_TEAM can create as many instances of the class C_BIKER as there are members in the team. Each instance of the class C_BIKER creates an instances of the class C_BICYCLE.

Each instance of the class C_TEAM can communicate with the program user through an interactive list. The program user can choose individual team members for actions. The instances of the class C_BIKER allow the program user to choose the action on a further selection screen.

This graphic is explained in the accompanying text

Constraints

Caution

The ABAP statements used for list processing are not yet fully available in ABAP Objects. However, to produce a simple test output, you can use the following statements:

Note: The behavior of formatting and interactive list functions in their current state are not guaranteed. Incompatible changes could occur in a future release.

Declarations

 

This example is implemented using local classes, since selection screens belong to an ABAP program, and cannot be defined or called in global classes. Below are the definitions of the two selection screens and three classes:

*******************************************************************
* Global Selection Screens
*******************************************************************

SELECTION-SCREEN BEGIN OF: SCREEN 100 TITLE TIT1, LINE.
  PARAMETERS MEMBERS TYPE I DEFAULT 10.
SELECTION-SCREEN END OF: LINE, SCREEN 100.

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

SELECTION-SCREEN BEGIN OF SCREEN 200 TITLE TIT2.
  PARAMETERS: DRIVE    RADIOBUTTON GROUP ACTN,
              STOP     RADIOBUTTON GROUP ACTN,
              GEARUP   RADIOBUTTON GROUP ACTN,
              GEARDOWN RADIOBUTTON GROUP ACTN.
SELECTION-SCREEN END OF SCREEN 200.

*******************************************************************
* Class Definitions
*******************************************************************

CLASS: C_BIKER DEFINITION DEFERRED,
       C_BICYCLE DEFINITION DEFERRED.

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

CLASS C_TEAM DEFINITION.

  PUBLIC SECTION.

  TYPES: BIKER_REF TYPE REF TO C_BIKER,
         BIKER_REF_TAB TYPE STANDARD TABLE OF BIKER_REF
                                           WITH DEFAULT KEY,

         BEGIN OF STATUS_LINE_TYPE,
           FLAG(1)  TYPE C,
           TEXT1(5) TYPE C,
           ID       TYPE I,
           TEXT2(7) TYPE C,
           TEXT3(6) TYPE C,
           GEAR     TYPE I,
           TEXT4(7) TYPE C,
           SPEED    TYPE I,
         END OF STATUS_LINE_TYPE.

  CLASS-METHODS: CLASS_CONSTRUCTOR.

  METHODS: CONSTRUCTOR,
           CREATE_TEAM,
           SELECTION,
           EXECUTION.

  PRIVATE SECTION.

  CLASS-DATA: TEAM_MEMBERS TYPE I,
              COUNTER TYPE I.

  DATA: ID TYPE I,
        STATUS_LINE TYPE STATUS_LINE_TYPE,
        STATUS_LIST TYPE SORTED TABLE OF STATUS_LINE_TYPE
                                      WITH UNIQUE KEY ID,

        BIKER_TAB TYPE BIKER_REF_TAB,
        BIKER_SELECTION LIKE BIKER_TAB,
        BIKER LIKE LINE OF BIKER_TAB.

  METHODS: WRITE_LIST.

ENDCLASS.

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

CLASS C_BIKER DEFINITION.

  PUBLIC SECTION.

  METHODS: CONSTRUCTOR IMPORTING TEAM_ID TYPE I MEMBERS TYPE I,
           SELECT_ACTION,
           STATUS_LINE EXPORTING LINE
                       TYPE C_TEAM=>STATUS_LINE_TYPE.

  PRIVATE SECTION.

  CLASS-DATA COUNTER TYPE I.

  DATA: ID TYPE I,
        BIKE TYPE REF TO C_BICYCLE,
        GEAR_STATUS  TYPE I VALUE 1,
        SPEED_STATUS TYPE I VALUE 0.

  METHODS BIKER_ACTION IMPORTING ACTION TYPE I.

ENDCLASS.

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

CLASS C_BICYCLE DEFINITION.

  PUBLIC SECTION.

  METHODS: DRIVE EXPORTING VELOCITY TYPE I,
           STOP  EXPORTING VELOCITY TYPE I,
           CHANGE_GEAR IMPORTING CHANGE TYPE I
                       RETURNING VALUE(GEAR) TYPE I
                       EXCEPTIONS GEAR_MIN GEAR_MAX.

  PRIVATE SECTION.

  DATA: SPEED TYPE I,
        GEAR  TYPE I VALUE 1.

  CONSTANTS: MAX_GEAR TYPE I VALUE 18,
             MIN_GEAR TYPE I VALUE 1.

ENDCLASS.

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

Note that none of the three classes has any public attributes. The states of the classes can only be changed by their methods. The class C_TEAM contains a static constructor CLASS_CONSTRUCTOR. C_TEAM and C_BIKER both contain instance constructors.

Implementations

The implementation parts of the classes contain the implementations of all of the methods declared in the corresponding declaration parts. The interfaces of the methods have already been defined in the declarations. In the implementations, the interface parameters behave like local data.

Methods of Class C_TEAM

The following methods are implemented in the section

CLASS C_TEAM IMPLEMENTATION.

...

ENDCLASS.

CLASS_CONSTRUCTOR

  METHOD CLASS_CONSTRUCTOR.
    TIT1 = 'Team members ?'.
    CALL SELECTION-SCREEN 100 STARTING AT 5 3.
    IF SY-SUBRC NE 0.
      LEAVE PROGRAM.
    ELSE.
      TEAM_MEMBERS = MEMBERS.
    ENDIF.
  ENDMETHOD.

The static constructor is executed before the class C_TEAM is used for the first time in a program. It calls the selection screen 100 and sets the static attribute TEAM_MEMBERS to the value entered by the program user. This attribute has the same value for all instances of the class C_TEAM.

 

CONSTRUCTOR

  METHOD CONSTRUCTOR.
    COUNTER = COUNTER + 1.
    ID = COUNTER.
  ENDMETHOD.

The instance constructor is executed directly after each instance of the class C_TEAM is created. It is used to count the number of instance of C_TEAM in the static attribute COUNTER, and assigns the corresponding number to the instance attribute ID of each instance of the class.

CREATE_TEAM

  METHOD CREATE_TEAM.
    DO TEAM_MEMBERS TIMES.
      CREATE OBJECT BIKER EXPORTING TEAM_ID = ID
                                    MEMBERS = TEAM_MEMBERS.
      APPEND BIKER TO BIKER_TAB.
      CALL METHOD BIKER->STATUS_LINE IMPORTING LINE = STATUS_LINE.
      APPEND STATUS_LINE TO STATUS_LIST.
    ENDDO.
  ENDMETHOD.

The public instance method CREATE_TEAM can be called by any user of the class containing a reference variable with a reference to an instance of the class. It is used to create instances of the class C_BIKER, using the private reference variable BIKER in the class C_TEAM. You must pass both input parameters for the instance constructor of class C_BIKER in the CREATE OBJECT statement. The references to the newly-created instances are inserted into the private internal table BIKER_TAB. After the method has been executed, each line of the internal table contains a reference to an instance of the class C_BIKER. These references are only visible within the class C_TEAM. External users cannot address the objects of class C_BIKER.

CREATE_TEAM also calls the method STATUS_LINE for each newly-created object, and uses the work area STATUS_LINE to append its output parameter LINE to the private internal table STATUS_LIST.

SELECTION

  METHOD SELECTION.
    CLEAR BIKER_SELECTION.
    DO.
      READ LINE SY-INDEX.
      IF SY-SUBRC <> 0. EXIT. ENDIF.
      IF SY-LISEL+0(1) = 'X'.
        READ TABLE BIKER_TAB INTO BIKER INDEX SY-INDEX.
        APPEND BIKER TO BIKER_SELECTION.
      ENDIF.
    ENDDO.
    CALL METHOD WRITE_LIST.
  ENDMETHOD.

The public instance method SELECTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. It selects all of the lines in the current list in which the checkbox in the first column is selected. For these lines, the system copies the corresponding reference variables from the table BIKER_TAB into an additional private internal table BIKER_SELECTION. SELECTION then calls the private method WRITE_LIST, which displays the list.

EXECUTION

  METHOD EXECUTION.
    CHECK NOT BIKER_SELECTION IS INITIAL.
    LOOP AT BIKER_SELECTION INTO BIKER.
      CALL METHOD BIKER->SELECT_ACTION.
      CALL METHOD BIKER->STATUS_LINE IMPORTING LINE = STATUS_LINE.
      MODIFY TABLE STATUS_LIST FROM STATUS_LINE.
    ENDLOOP.
    CALL METHOD WRITE_LIST.
  ENDMETHOD.

The public instance method EXECUTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method calls the two methods SELECT_ACTION and STATUS_LINE for each instance of the class C_BIKER for which there is a reference in the table BIKER_SELECTION. The line of the table STATUS_LIST with the same key as the component ID in the work area STATUS_LINE is overwritten and displayed by the private method WRITE_LIST.

WRITE_LIST

  METHOD WRITE_LIST.
    SET TITLEBAR 'TIT'.
    SY-LSIND = 0.
    SKIP TO LINE 1.
    POSITION 1.
    LOOP AT STATUS_LIST INTO STATUS_LINE.
      WRITE: / STATUS_LINE-FLAG AS CHECKBOX,
               STATUS_LINE-TEXT1,
               STATUS_LINE-ID,
               STATUS_LINE-TEXT2,
               STATUS_LINE-TEXT3,
               STATUS_LINE-GEAR,
               STATUS_LINE-TEXT4,
               STATUS_LINE-SPEED.
    ENDLOOP.
  ENDMETHOD.

The private instance method WRITE_LIST can only be called from the methods of the class C_TEAM. It is used to display the private internal table STATUS_LIST on the basic list (SY-LSIND = 0) of the program.

Methods of Class C_BIKER

The following methods are implemented in the section

CLASS C_BIKER IMPLEMENTATION.

...

ENDCLASS.

CONSTRUCTOR

  METHOD CONSTRUCTOR.
    COUNTER = COUNTER + 1.
    ID = COUNTER - MEMBERS * ( TEAM_ID - 1).
    CREATE OBJECT BIKE.
  ENDMETHOD.

The instance constructor is executed directly after each instance of the class C_BIKER is created. It is used to count the number of instance of C_BIKER in the static attribute COUNTER, and assigns the corresponding number to the instance attribute ID of each instance of the class. The constructor has two input parameters - TEAM_ID and MEMBERS - which you must pass in the CREATE OBJECT statement when you create an instance of C_BIKER.

The instance constructor also creates an instance of the class C_BICYCLE for each new instance of the class C_BIKER. The reference in the private reference variable BIKE of each instance of C_BIKER points to a corresponding instance of the class C_BICYCLE. No external user can address these instances of the class C_BICYCLE.

SELECT_ACTION

  METHOD SELECT_ACTION.
    DATA ACTIVITY TYPE I.
    TIT2 = 'Select action for BIKE'.
    TIT2+24(3) = ID.
    CALL SELECTION-SCREEN 200 STARTING AT 5 15.
    CHECK NOT SY-SUBRC GT 0.
    IF GEARUP = 'X' OR GEARDOWN = 'X'.
      IF GEARUP = 'X'.
        ACTIVITY = 1.
      ELSEIF GEARDOWN = 'X'.
        ACTIVITY = -1.
      ENDIF.
    ELSEIF DRIVE = 'X'.
      ACTIVITY = 2.
    ELSEIF STOP = 'X'.
      ACTIVITY = 3.
    ENDIF.
    CALL METHOD BIKER_ACTION( ACTIVITY).
  ENDMETHOD.

The public instance method SELECT_ACTION can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method calls the selection screen 200 and analyzes the user input. After this, it calls the private method BIKER_ACTION of the same class. The method call uses the shortened form to pass the actual parameter ACTIVITY to the formal parameter ACTION.

BIKER_ACTION

  METHOD BIKER_ACTION.
    CASE ACTION.
      WHEN -1 OR 1.
        CALL METHOD BIKE->CHANGE_GEAR
                          EXPORTING CHANGE = ACTION
                          RECEIVING GEAR = GEAR_STATUS
                          EXCEPTIONS GEAR_MAX = 1
                                     GEAR_MIN = 2.
        CASE SY-SUBRC.
          WHEN 1.
            MESSAGE I315(AT) WITH 'BIKE' ID
                                  ' is already at maximal gear!'.
          WHEN 2.
            MESSAGE I315(AT) WITH 'BIKE' ID
                                  ' is already at minimal gear!'.
        ENDCASE.
      WHEN 2.
        CALL METHOD BIKE->DRIVE IMPORTING VELOCITY = SPEED_STATUS.
      WHEN 3.
        CALL METHOD BIKE->STOP IMPORTING VELOCITY = SPEED_STATUS.
    ENDCASE.
  ENDMETHOD.

The private instance method BIKER_ACTION can only be called from the methods of the class C_BIKER. The method calls other methods in the instance of the class C_BICYCLE to which the reference in the reference variable BIKE is pointing, depending on the value in the input parameter ACTION.

STATUS_LINE

  METHOD STATUS_LINE.
    LINE-FLAG = SPACE.
    LINE-TEXT1 = 'Biker'.
    LINE-ID = ID.
    LINE-TEXT2 = 'Status:'.
    LINE-TEXT3 = 'Gear = '.
    LINE-GEAR  = GEAR_STATUS.
    LINE-TEXT4 = 'Speed = '.
    LINE-SPEED = SPEED_STATUS.
  ENDMETHOD.

The public instance method STATUS_LINE can be called by any user of the class containing a reference variable with a reference to an instance of the class. It fills the structured output parameter LINE with the current attribute values of the corresponding instance.

Methods of Class C_BICYCLE

The following methods are implemented in the section

CLASS C_BICYCLE IMPLEMENTATION.

...

ENDCLASS.

DRIVE

  METHOD DRIVE.
    SPEED = SPEED  + GEAR * 10.
    VELOCITY = SPEED.
  ENDMETHOD.

The public instance method DRIVE can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute SPEED and passes it to the caller using the output parameter VELOCITY.

STOP

  METHOD STOP.
    SPEED = 0.
    VELOCITY = SPEED.
  ENDMETHOD.

The public instance method STOP can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute SPEED and passes it to the caller using the output parameter VELOCITY.

CHANGE_GEAR

  METHOD CHANGE_GEAR.
    GEAR = ME->GEAR.
    GEAR = GEAR + CHANGE.
    IF GEAR GT MAX_GEAR.
      GEAR = MAX_GEAR.
      RAISE GEAR_MAX.
    ELSEIF GEAR LT MIN_GEAR.
      GEAR = MIN_GEAR.
      RAISE GEAR_MIN.
    ENDIF.
    ME->GEAR = GEAR.
  ENDMETHOD.

The public instance method CHANGE_GEAR can be called by any user of the class containing a reference variable with a reference to an instance of the class. The method changes the value of the private attribute GEAR. Since the formal parameter with the same name obscures the attribute in the method, the attribute has to be addressed using the self-reference ME->GEAR.

 

Using the Classes in a Program

The following program shows how the above classes can be used in a program. The declarations of the selection screens and local classes, and the implementations of the methods must also be a part of the program.

REPORT OO_METHODS_DEMO NO STANDARD PAGE HEADING.

*******************************************************************
* Declarations and Implementations
*******************************************************************

...

*******************************************************************
* Global Program Data
*******************************************************************

TYPES TEAM TYPE REF TO C_TEAM.

DATA: TEAM_BLUE  TYPE TEAM,
      TEAM_GREEN TYPE TEAM,
      TEAM_RED   TYPE TEAM.

DATA  COLOR(5).

*******************************************************************
* Program events
*******************************************************************

START-OF-SELECTION.

  CREATE OBJECT: TEAM_BLUE,
                 TEAM_GREEN,
                 TEAM_RED.

   CALL METHOD: TEAM_BLUE->CREATE_TEAM,
               TEAM_GREEN->CREATE_TEAM,
               TEAM_RED->CREATE_TEAM.

  SET PF-STATUS 'TEAMLIST'.

  WRITE '                   Select a team!             ' COLOR = 2.

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

AT USER-COMMAND.
  CASE SY-UCOMM.
    WHEN 'TEAM_BLUE'.
      COLOR = 'BLUE '.
      FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.
      
CALL METHOD TEAM_BLUE->SELECTION.
    WHEN 'TEAM_GREEN'.
      COLOR = 'GREEN'.
      FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.
      
CALL METHOD TEAM_GREEN->SELECTION.
    WHEN 'TEAM_RED'.
      COLOR = 'RED '.
      FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.
      
CALL METHOD TEAM_RED->SELECTION.
    WHEN 'EXECUTION'.
      CASE COLOR.
        WHEN 'BLUE '.
          FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.
          
CALL METHOD TEAM_BLUE->SELECTION.
          CALL METHOD TEAM_BLUE->EXECUTION.

        WHEN 'GREEN'.
          FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.
          
CALL METHOD TEAM_GREEN->SELECTION.
          CALL METHOD TEAM_GREEN->EXECUTION.

        WHEN 'RED '.
          FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.
          
CALL METHOD TEAM_RED->SELECTION.
          CALL METHOD TEAM_RED->EXECUTION.

      ENDCASE.
  ENDCASE.

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

The program contains three class reference variables that refer to the class C_TEAM. It creates three objects from the class, to which the references in the reference variables then point. In each object, it calls the method CREATE_TEAM. The method CLASS_CONSTRUCTOR of class C_TEAM is executed before the first of the objects is created. The status TEAMLIST for the basic list allows the user to choose one of four functions:

This graphic is explained in the accompanying text

When the user chooses a function, the event AT USER-COMMAND is triggered and public methods are called in one of the three instances of C_TEAM, depending on the user’s choice. The user can change the state of an object by selecting the corresponding line in the status list.