How To Consume Methods From Another ABAP Program?

When you create a class object using Transaction Code SE24, the class is stored in the ABAP repository and becomes reusable by other developers across various programs. These classes object are part of the global class pool.

However, what happens to classes that are defined locally within an ABAP program? Local classes are restricted to the scope of the program they are defined in, which limits their reusability directly. But don’t worry because ABAP provides a way that make it possible to consume these local classes and their methods from other programs.

In this tutorial, I will demonstrate how easy it is to dynamically access and utilize classes and methods defined within another ABAP program.

Tutorial Objectives
1. Learn how to define and instantiate classes from another ABAP program.
2. You’re able to reuse and execute methods from another ABAP program.
Prerequisites
1. SAP System Access: SAP GUI, ECC.
2. Authorization: Developer role, TCODE: SE38.

Scenario Setup

To demonstrate how to dynamically consume classes and methods from another ABAP program, we’ll create two programs:

  1. Base Program (ZABAP01): This program defines the class and its methods.
  2. Consumer Program (ZABAP02): This program dynamically interacts with the class and methods from ZABAP01.

Here’s how we construct the two ABAP programs

  1. ZABAP01 consists of one class (CL_ABAP_01) and two methods (GET_DATA) and (DISPLAY) with one parameter P_CURRCD. The program will get carriers data from the SCARR table based on the given currency code parameter, and then display it on the screen.
  2. ZABAP02 will redefine/call the CLASS and METHODS located in ZABAP01, after that get the carriers data just like in ZABAP01 program.

The Step By Step Implementation To Consume Methods From Other ABAP Program

Step 1. Create a new ABAP program and then copy this source code below.

report zabap01.
*&---------------------------------------------------------------------*
*& Report ZABAP01
*&---------------------------------------------------------------------*
*& ABAPGeeks.com
*&---------------------------------------------------------------------*

* This is a demo program to demonstrate how to
* consume class from one program to another.
*Here wwe define a class object to get carriers data
*consists of 2 methods (get_data, display)
*In another program which is ZABAP02, we will try
*to consume these methods.

class cl_abap_01 definition.
  public section.
    methods:
      get_data importing i_currcode type s_currcode
               exporting e_row      type i,
      display.
  protected section.
  private section.
    data: it_scarr type table of scarr.

endclass.

*This is the parameter to get carriers data
*based on their curruncy code
parameters: p_currcd type s_currcode.

start-of-selection.
*Here we instantiate the class object
  data(o_abap01) = new cl_abap_01( ).
*We call the GET_DATA method, passing the CURRENCY parameter
  o_abap01->get_data(
               exporting
                 i_currcode = p_currcd
               importing e_row = data(e_row) ).
  if e_row = 0.
*IF the carriers data exist then display the table
    o_abap01->display( ).
  endif.

*Here's the implementation class
class cl_abap_01 implementation.
  method get_data.

    select * from scarr
       into table it_scarr
       where currcode = i_currcode.

    e_row = sy-subrc.

  endmethod.
  method display.
      cl_demo_output=>display( it_scarr ). "display the carriers result
  endmethod.
endclass.

Save and Activate.

If you run the program and input the currency parameter then you will get this result.

Step 2. Create the second program (ZABAP02) where we will consume the class and methods from the first program (ZABAP01).

*&---------------------------------------------------------------------*
*& Report ZABAP02
*&---------------------------------------------------------------------*
*& ABAPGeeks.com
*&---------------------------------------------------------------------*

"This second program will call the methods from ZABAP01.
report zabap02.

data: e_row type sy-subrc.
data cl_abap01 type ref to object.
*The string name of the class object in program ZABAP01
data(lv_clsnm) = '\PROGRAM=ZABAP01\CLASS=CL_ABAP_01'.

parameters: p_curr type s_currcode.

start-of-selection.
*instantiate the class object using dynamic type.
  create object cl_abap01 type (lv_clsnm).
*call the GET_DATA method from ZABAP01
  call method cl_abap01->('GET_DATA')
    exporting
      i_currcode = p_curr
    importing
      e_row      = e_row.
*call the DISPlAY method from ZABAP01
    if e_row = 0.
      call method cl_abap01->('DISPLAY').
    endif.
    

If you run the program then it will execute the methods defined in the first program (ZABAP01).

Below is the result for EUR currency code.

Note: Unfortunately static methods (CLASS-METHODS) cannot be called using this technique.

Final Thought

While this technique gives a clever way to reuse local classes defined within ABAP programs, it’s essential to ensure that the base program remains stable and accessible. Because I like to categorized this technique as a “TRICK“, Why? Because when the base program was originally created, it’s almost certain there wasn’t any intent to make its local classes reusable by other programs, so any changes or deactivation of the base program can lead to runtime errors or unexpected behavior.

Ace your next ABAP Job interview with the TOP 30 ABAP Interview Questions.

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *