Local class with user-defined types in SAP OOABAP

Defining a local class with user defined types in SAP ABAP programming, local classes with user defined table types

+ -

It is very important for us to use user-defined types when using local classes in SAP ABAP programs, the below example explains using user-defined types in SAP ABAP programs.

The below example explains you of using user defined types in SAP Local classes in SAP ABAP programming.

Declare a table type

Declare a table type with required fields
TYPES : BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MEINS TYPE MARA-MEINS,
        MATKL TYPE MARA-MATKL,
        END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.

Define class and method

CLASS CL_USERDEFINED_TYPES DEFINITION.
  PUBLIC SECTION.
    METHODS : GET_MATERIALS_FOR_TYPE
              IMPORTING IM_MTART TYPE MARA-MTART
              EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.

Implement the defined class

CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
  METHOD GET_MATERIALS_FOR_TYPE.
    SELECT MATNR MTART MEINS MATKL FROM MARA
           INTO TABLE ET_MARA
           WHERE MTART = IM_MTART .
  ENDMETHOD.
ENDCLASS.

Use the class and method

Create an object for the class and call the method
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
  EXPORTING
    IM_MTART = P_MTART
  IMPORTING
    ET_MARA  = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.

The final code will be

REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.

PARAMETERS : P_MTART TYPE MARA-MTART.
DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
TYPES : BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MEINS TYPE MARA-MEINS,
        MATKL TYPE MARA-MATKL,
        END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
  PUBLIC SECTION.
    METHODS : GET_MATERIALS_FOR_TYPE
              IMPORTING IM_MTART TYPE MARA-MTART
              EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
  EXPORTING
    IM_MTART = P_MTART
  IMPORTING
    ET_MARA  = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
  METHOD GET_MATERIALS_FOR_TYPE.
    SELECT MATNR MTART MEINS MATKL FROM MARA
           INTO TABLE ET_MARA
          UP TO 50 ROWS
           WHERE MTART = IM_MTART .
  ENDMETHOD.
ENDCLASS.

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

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


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Events in Local Classes in SAP OOABAP