Mastering SAP ABAP Interfaces: From Definition to Implementation

 

Interfaces in SAP ABAP :-

  • Interfaces provide a standard way to define methods that can be used across different classes.
  • In Interface all methods are public by default.
  • In Interface all methods are abstract methods ( i.e. There is only definition, no implementation ).

Difference between Abstract Classes and Interfaces :-

1. Abstract Class :-

  • In Abstract class, we have at least one abstract method, others can be non-abstract methods.
  • Methods visibility can be Public, Private and Protected.
  • We need to redefine the abstract method in to sub classes to perform the implementation in Sub class
  • Multiple Inheritance is not possible.

Important Point :-

  • We can not redefine a static method.

2. Interfaces :-

  • All methods are abstract methods in Interface.
  • All methods are public.
  • There is no need to click on the redefine button ( redefine button is disabled ), we can write the logic in class by double.
  • Multiple Inheritance is possible.

  • let’s take a requirement, where we create a interface with one method and then, we will implement that Interface in another class.
  • Our requirement is to display Sales Order Details from VBAK table on the basis of Sales Document Number.

How to create a Interface in Usual ABAP / Global Class (SE24 ) ?

  • Step 1 :- Go to SE24 transaction code.

  • Step 2 :- Give a name to the interface and click on create button.



  • Step 3 :- A popup screen will appear, select the Interface radio button.



    • Press Enter.
  • Step 4 :- Provide a description for the interface.



  • Step 5 :- Our Interface Screen will appear.



  • Step 6 :- Here we will give the name of our method.

    • Note :- You can see in the above diagram that Visibility option is not present in case of Interface.


    • Click on Parameters and provide the importing and exporting parameters.



    • Save and activate the interface.




  • Step 7 :- Now, we need to implement the Interface in a class, So create a class from SE24 transaction code.



  • Step 8 :- click on create button and select Class radio button.



    • Press enter.


  • Step 9 :- Select the Interface tab.



  • Step 10 :- Fill the details as shown below.



    • Now click on Methods tab, you can see that the Method of interface is already assigned here.


  • Step 11 :- Click on source code and write the logic.



    • Activate the class.

Execute the Class :-

  • Press F8 to execute the class.



  • Press F8.



    • You can see it, is perfectly working.

Let’s achieve the above same to same requirement using local classes and Interfaces through ABAP Editor ( SE38 ).

Implementing Interface Using Program in ABAP :-

Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_INTERFACE_IMPLEMENT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_INTERFACE_IMPLEMENT.

DATA : perdat type erdat,
       perzet type erzet,
       pernam type ernam,
       pvbtyp type vbtypl.

PARAMETERS : p_vbeln type vbeln_va.

INTERFACE Sales_Order.
  METHODS DISPLAY IMPORTING pvbeln type vbeln_va
                  EXPORTING perdat type erdat
                            perzet type erzet
                            pernam type ernam
                            pvbtyp type vbtypl.

ENDINTERFACE.

CLASS Sales_Order_IMPLEMENTATION DEFINITION.
  PUBLIC SECTION.
  INTERFACES Sales_Order.

ENDCLASS.

CLASS Sales_Order_IMPLEMENTATION IMPLEMENTATION.
  Method Sales_Order~DISPLAY.
    SELECT single erdat erzet ernam vbtyp
      from vbak into ( perdat, perzet, pernam, pvbtyp ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA(lo_object) = new Sales_Order_IMPLEMENTATION( ).
lo_object->Sales_Order~DISPLAY( EXPORTING pvbeln = p_vbeln
                                IMPORTING perdat = perdat
                                          perzet = perzet
                                          pernam = pernam
                                          pvbtyp = pvbtyp ).

WRITE :/ perdat, perzet, pernam, pvbtyp.

*&------------------------------------------------------------------------
*&End of Program
*&--------------------------------------------------------------

Execute the Code :-



  • Press F8.


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP