Multiple Inheritance Using Interfaces in ABAP

 

  • In ABAP Object Oriented Programming, we cannot achieve Multiple inheritance using Normal Class or Abstract Class because we can only pass one class as a super Class.

    • We can see this in SE24 ( Class Builder ).


    • As we can see, we can only pass one parameter at a time.

    • However, we can pass multiple interfaces in a class.



    • and by using this mechanism we can achieve Multiple inheritance using Interfaces in ABAP.


let’s take a requirement and implement it practically.

Requirement :-

  • We have to create two radio buttons and one Select option Input for Sales Document Number on the Selection Screen.
  • The Customer wants that we should use two interfaces and implement it inside one class.
  • IF Sales radio button is selected then It should be displaying VBAK details.
  • Else If Billing Radio button is selected then it should be displaying VBRK details.

Solution :-

  • Step 1 :- Go to transaction code SE38 ( ABAP Editor ) and create a executable program.



  • Step 2 :- Create internal table for VBAK and VBRK table.



  • Step 3 :- Define two radio buttons and one select option for user input.



  • Step 4 :- Create Sales interface and Billing Interface with one method.



  • Step 5 :- Create a class and implement these above methods.



  • Step 6 :- In start of selection create object for the class.



  • Step 7 :- Call the method according to the radio button and use factory method of CL_SALV_TABLE class to display in form of ALV.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_MULTIPLE_INHERITANCE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_MULTIPLE_INHERITANCE.

DATA : lt_vbak type table of vbak,
       lt_vbrk type table of vbrk.

DATA : lv_vbeln type vbeln_va.
SELECT-OPTIONS : s_vbeln for lv_Vbeln.
PARAMETERS : p_rad1 type c RADIOBUTTON GROUP r1,
             p_rad2 type c RADIOBUTTON GROUP r1.

INTERFACE Sales.
  METHODS DISPLAY_SALES_ORDER.

ENDINTERFACE.

INTERFACE Billing.
  METHODS DISPLAY_BILLING.

ENDINTERFACE.

CLASS ORDER DEFINITION.
  PUBLIC SECTION.
  INTERFACES : Sales,
             Billing.

ENDCLASS.
CLASS ORDER IMPLEMENTATION.
  METHOD SALES~display_sales_order.
    select * from vbak
      into table lt_vbak
      where vbeln in s_vbeln.
  ENDMETHOD.

  METHOD BILLING~display_billing.
    select * from vbrk
      into table lt_vbrk
      where vbeln in s_vbeln.
  endmethod.
ENDCLASS.

START-OF-SELECTION.
DATA(lo_object) = new ORDER( ).

if p_rad1 = 'X'.
  lo_object->Sales~display_sales_order( ).

  TRY.
  CALL METHOD cl_salv_table=>factory
    EXPORTING
      list_display   = IF_SALV_C_BOOL_SAP=>FALSE
    IMPORTING
      r_salv_table   = data(lo_disp1)
    CHANGING
      t_table        = lt_vbak
      .
    CATCH cx_salv_msg.
  ENDTRY.
  lo_disp1->display( ).

elseif p_rad2 = 'X'.

  lo_object->Billing~display_billing( ).

  TRY.
  CALL METHOD cl_salv_table=>factory
    EXPORTING
      list_display   = IF_SALV_C_BOOL_SAP=>FALSE
    IMPORTING
      r_salv_table   = data(lo_disp2)
    CHANGING
      t_table        = lt_vbrk
      .
    CATCH cx_salv_msg.
  ENDTRY.
  lo_disp2->display( ).

endif.

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

Execute the code:-

1. Select Sales Order Display radio button



  • Press F8.



  • Now go back.

2. Select Billing Display Radio button.



  • Press F8.



Comments

Popular posts from this blog

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

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

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