Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

Interface - Type of ABAP Objects

Interface is a data type of ABAP objects. Hence the declaration of interface is similar to a class. We can define attributes and methods in Interface. This method can be implemented in any class implementation because Interface cannot be implemented. We use interface when we need to declare same methods in different classes. Here the name of the method can be similar to any class method. So if we declare a method meth in Interface then this meth can be declared again in any class. Here the system will treat two different methods. Functionally these two methods are completely different. The method meth can be reused in different classes as per the requirement. Hence Interface can create reusable methods through the entire program.

Here is a program where we want to show the Purchase order Item wise information. The output will contain Purchase Order (EBELN), Item (EBELP), Material (MATNR), Plant (WERKS), Storage Location (LGORT), Company Code (BUKRS) and Vendor (LIFNR). The logic behind this will be selecting data from EKKO and then Selection data from EKPO based on the Select Option. So two internal tables will be defined and joined by For All Entries condition.

Here we have defined an Interface intfc. We define a text attribute v_txt and a method meth in this interface. This method can be implemented by any class implementation. So we have defined a class cls. Under the Public Section we define a method which is having the same name meth. Here meth will be treated as the method of class cls, not the method of Interface. Then we have defined interface intfc in this public section. We also declare table types of internal tables here.

Now we are implementing the class cls with the method of interface and of its own. At the time of implementing the Interface method we use the statement METHOD intfc~meth and ENDMETHOD. In this method we Select data from EKKO into internal table it_ekko. Then Select data from EKPO into internal table it_ekpo for all entries in it_ekko. After that we implement the class method where we are preparing the final internal table to be displayed in Output.

Finally in the Start of selection we create an object obj and initialize a value to the Interface attribute. The statement is obj->intfc~v_txt = 'Purchase Order Information'. Then we Write obj->intfc~v_txt. After that we are calling the Interface method and the class method. Statement is CALL METHOD: obj->intfc~meth, obj->meth.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

TABLES: ekko, ekpo.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

*----------------------------------------------------------------------*
*       INTERFACE int
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE intfc.

  DATA v_txt TYPE char50.
  METHODS meth.

ENDINTERFACE.                    "int

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.

    TYPES: BEGIN OF ty_ekko,
            ebeln TYPE ekko-ebeln,
            bukrs TYPE ekko-bukrs,
            lifnr TYPE ekko-lifnr,
           END OF ty_ekko,

           BEGIN OF ty_ekpo,
            ebeln TYPE ekpo-ebeln,
            ebelp TYPE ekpo-ebelp,
            matnr TYPE ekpo-matnr,
            werks TYPE ekpo-werks,
            lgort TYPE ekpo-lgort,
           END OF ty_ekpo,

           BEGIN OF ty_out,
            ebeln TYPE ekpo-ebeln,
            ebelp TYPE ekpo-ebelp,
            matnr TYPE ekpo-matnr,
            werks TYPE ekpo-werks,
            lgort TYPE ekpo-lgort,
            bukrs TYPE ekko-bukrs,
            lifnr TYPE ekko-lifnr,
           END OF ty_out.

    DATA: wa_ekko TYPE ty_ekko,
          wa_ekpo TYPE ty_ekpo,
          wa_out  TYPE ty_out,
          it_ekko TYPE STANDARD TABLE OF ty_ekko,
          it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
          it_out  TYPE STANDARD TABLE OF ty_out.

    METHODS meth.
    INTERFACES intfc.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.

  METHOD intfc~meth.

    SELECT ebeln bukrs lifnr FROM ekko INTO TABLE it_ekko
    WHERE ebeln IN s_ebeln.

    IF sy-subrc = 0.
      SORT it_ekko BY ebeln.

      SELECT ebeln ebelp matnr werks lgort
      FROM ekpo INTO TABLE it_ekpo
      FOR ALL ENTRIES IN it_ekko
      WHERE ebeln = it_ekko-ebeln.

      IF sy-subrc = 0.
        SORT it_ekpo BY ebeln ebelp.
      ENDIF.

    ELSE.
      MESSAGE 'Purchase Order Doesn''t Exist' TYPE 'I'.
      LEAVE LIST-PROCESSING.
    ENDIF.

  ENDMETHOD.                    "int~meth

  METHOD meth.

    IF it_ekpo IS NOT INITIAL.
      LOOP AT it_ekpo INTO wa_ekpo.
        wa_out-ebeln = wa_ekpo-ebeln.
        wa_out-ebelp = wa_ekpo-ebelp.
        wa_out-matnr = wa_ekpo-matnr.
        wa_out-werks = wa_ekpo-werks.
        wa_out-lgort = wa_ekpo-lgort.

        READ TABLE it_ekko INTO wa_ekko
        WITH KEY ebeln = wa_ekpo-ebeln BINARY SEARCH.
        IF sy-subrc = 0.
          wa_out-bukrs = wa_ekko-bukrs.
          wa_out-lifnr = wa_ekko-lifnr.
        ENDIF.

        APPEND wa_out TO it_out.
        CLEAR: wa_out, wa_ekko, wa_ekpo.
      ENDLOOP.
    ENDIF.

    IF it_out IS NOT INITIAL.
      LOOP AT it_out INTO wa_out.
        AT FIRST.
          WRITE: / 'Purchase Order',
                18 'Item',
                26 'Material',
                48 'Plant',
                55 'Storage',
                65 'Company',
                75 'Vendor'.
          ULINE.
          SKIP.
        ENDAT.

        WRITE: / wa_out-ebeln,
              18 wa_out-ebelp,
              26 wa_out-matnr,
              48 wa_out-werks,
              55 wa_out-lgort,
              65 wa_out-bukrs,
              75 wa_out-lifnr.
      ENDLOOP.
    ENDIF.

  ENDMETHOD.                    "meth
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.

  obj->intfc~v_txt = 'Purchase Order Information'.
  WRITE / obj->intfc~v_txt.
  ULINE.
  SKIP 2.

  CALL METHOD: obj->intfc~meth,
               obj->meth.


The following is the Output:


Output:

No comments:

Post a Comment