Local class with methods in SAP OOABAP

Local class with methods in SAP ABAP, defining methods in local classes, implementing methods in SAP local classes

+ -

Local class is a class definition and implementation is available in a program.

Go to SE38, create a program ZSAPN_CLASS_METHODS1 and follow steps to add code

Define a class

CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE 

IMPLEMENTATION
PUBLIC SECTION.
METHODS : GET_MATERAIAL_DETAILS
          IMPORTING IM_MATNR TYPE MATNR
          EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
          IMPORTING IM_MATNR TYPE MATNR
          EXPORTING EX_MARA TYPE MARA.
       
ENDCLASS.

Implement class

CLASS CL_METHODS_EXAMPLE IMPLEMENTATION.
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
     INTO EX_MARA
     WHERE MATNR = IM_MATNR.
ENDMETHOD.

METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
       INTO EX_MAKT
       WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.

Using class

DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS
            EXPORTING
            IM_MATNR = P_MATNR
            IMPORTING
            EX_MARA = WA_MARA .


CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTION
            EXPORTING
            IM_MATNR = P_MATNR
            IMPORTING
            EX_MAKT = WA_MAKT .


*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MAKTL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

DEFINITION DEFERED is a kwyword which indicates the class definition is delayed or postponed or definition at some place in program.

Final code will be

REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_METHODS_EXAMPLE  DEFINITION DEFERRED.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS


CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE IMPLEMENTATION
  PUBLIC SECTION.
    METHODS : GET_MATERIAL_DETAILS
              IMPORTING IM_MATNR TYPE MATNR
              EXPORTING EX_MARA TYPE MARA.
    CLASS-METHODS : GET_MATERIAL_DESCRIPTION
              IMPORTING IM_MATNR TYPE MATNR
              EXPORTING EX_MAKT TYPE MAKT.

ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS
  EXPORTING
    IM_MATNR = P_MATNR
  IMPORTING
    EX_MARA  = WA_MARA.


CALL METHOD CL_METHODS_EXAMPLE=>GET_MATERIAL_DESCRIPTION
  EXPORTING
    IM_MATNR = P_MATNR
  IMPORTING
    EX_MAKT  = WA_MAKT.


*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

CLASS CL_METHODS_EXAMPLE IMPLEMENTATION.
  METHOD GET_MATERIAL_DETAILS.
    SELECT SINGLE * FROM MARA
         INTO EX_MARA
         WHERE MATNR = IM_MATNR.
  ENDMETHOD.

  METHOD GET_MATERIAL_DESCRIPTION.
    SELECT * FROM MAKT
           INTO EX_MAKT
           WHERE MATNR = IM_MATNR.
    ENDSELECT.
  ENDMETHOD.
ENDCLASS.

Please Sign in to ask a question

Was this lesson helpful to you? Yes No 20 People out of 20 think this lesson helpful


Lesson Navigation

Local Classes in SAP OOABAP← Previous Lesson
Next Chapter →