Creating and Using interfaces in Local Classes in SAP OOABAP

Creating a class interface locally and using it in a local class program - SAP ABAP programming

+ -

The below example explains you using interface concept in SAP ABAP local classes, most of the times we don`t prefer interfaces in local classes, any how we will learn how to use them in local calsses.

The below syntax is used to declare interface in local classes.
INTERFACE  .
**Define methods and events
ENDINTERFACE.
To implement the local interface in a local class, we use below syntax.
CLASS  DEFINITION.
  PUBLIC SECTION. "visibility
    INTERFACES  .
ENDCLASS.

In the below example we are creating and using interfaces locally, please go through the example of creating and using interface in global classes , the same we are doing in a local class.

Follow the below steps to create and use interfaces in local classes.

Define an interface

Define an interface and add methods which dosen`t need any implementation.
INTERFACE CL_INTERFACE.
  METHODS : GET_MATERIAL_DETAILS
            IMPORTING IM_MATNR TYPE MARA-MATNR
            EXPORTING EX_MARA TYPE MARA.
  METHODS : GET_MATERIAL_DESCRIPTIONS
            IMPORTING IM_MATNR TYPE MARA-MATNR
            EXPORTING EX_MAKT TYPE MAKT.
ENDINTERFACE.

Define a class and add interface

Define a local class and add the above interface into it .
CLASS CL_CLASS DEFINITION.
  PUBLIC SECTION.
    INTERFACES CL_INTERFACE.
ENDCLASS.

Implement the methods in local class

Implement the methods of interface in the defined local class.
CLASS CL_CLASS IMPLEMENTATION.

  METHOD CL_INTERFACE~GET_MATERIAL_DETAILS.
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.

  ENDMETHOD.
  METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
    SELECT * FROM MAKT
      INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
    ENDSELECT.

  ENDMETHOD.

ENDCLASS.               "cl_clsss

Creat object and use it in the program

Now the class is created and implemented, we can use the call methods .
DATA LO_CLASS TYPE REF TO CL_CLASS.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
START-OF-SELECTION.
  CREATE OBJECT LO_CLASS.

  CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.

  WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

  WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

The final program source will be

REPORT ZSAPN_LOCAL_CLASS_INTERFACE.

CLASS CL_CLASS DEFINITION DEFERRED.
DATA LO_CLASS TYPE REF TO CL_CLASS.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.

INTERFACE CL_INTERFACE.
  METHODS : GET_MATERIAL_DETAILS
            IMPORTING IM_MATNR TYPE MARA-MATNR
            EXPORTING EX_MARA TYPE MARA.
  METHODS : GET_MATERIAL_DESCRIPTIONS
            IMPORTING IM_MATNR TYPE MARA-MATNR
            EXPORTING EX_MAKT TYPE MAKT.
ENDINTERFACE.

CLASS CL_CLASS DEFINITION.
  PUBLIC SECTION.
    INTERFACES CL_INTERFACE.
ENDCLASS.

START-OF-SELECTION.
  CREATE OBJECT LO_CLASS.

  CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.

  WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

  WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
CLASS CL_CLASS IMPLEMENTATION.

  METHOD CL_INTERFACE~GET_MATERIAL_DETAILS.
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.

  ENDMETHOD.
  METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
    SELECT * FROM MAKT
      INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
    ENDSELECT.

  ENDMETHOD.

ENDCLASS.               "cl_clsss

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 18 People out of 19 think this lesson helpful


    Lesson Navigation

    Local class constructor in OOABAP← Previous Lesson
    Next Lesson → Inheritance in local classes in SAP OOABAP