Local Classes in ABAP

In ABAP, you might have learned to create global classes using SE24, Transaction code. But some time, there is a requirement to create a local class and its method call altogether in a report. In this article, we will learn how to implement Local Classes in ABAP.

Introduction

Local Classes in ABAP are just like classes of Programming language C/C++. We define following while doing Local classes implementation in ABAP:

  • Class: We define a class with different section: Public, Private and Protected
  • Method: We define Importing, Exporting and Exception parameters for a Method
  • Create Object: This we create for our Global classes too, used to create an object for a Class in our regular ABAP programs
  • CALL METHOD: To call the methods of class, using the object created above.
Local Classes in ABAP - Image Illustration
Local Class Implementation in ABAP – Image Illustration

 

ABAP Program

Program Requirement: Get basic and salary details of an employee from two different classes. Take Employee ID as input.

CLASS class_test DEFINITION DEFERRED.
PARAMETERS: p_empid TYPE char8.
DATA: wa_emp  TYPE zBarry_emp,
      wa_emp2 TYPE zBarry_sal.
DATA: obj TYPE REF TO class_test.

INTERFACE interface.
  METHODS: method2
    IMPORTING imp2 TYPE char8
    EXPORTING exp2 TYPE zBarry_sal.
ENDINTERFACE.

CLASS class_test DEFINITION.
  PUBLIC SECTION.
    EVENTS: event1.
    INTERFACES: interface.
    METHODS: method1
      IMPORTING imp TYPE char8
      EXPORTING exp TYPE zBarry_emp.
    METHODS: eventhandler FOR EVENT event1 OF class_test.

ENDCLASS.

CREATE OBJECT obj.

SET HANDLER obj->eventhandler FOR obj.
CALL METHOD obj->method1
  EXPORTING
    imp = p_empid
  IMPORTING
    exp = wa_emp.

CALL METHOD obj->interface~method2
  EXPORTING
    imp2 = p_empid
  IMPORTING
    exp2 = wa_emp2.

WRITE:/ wa_emp.
write:/ wa_emp2-empid, wa_emp2-tid,wa_emp2-mon.
*&---------------------------------------------------------------------*
*&       Class (Implementation)  class_test
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
CLASS class_test IMPLEMENTATION.
  METHOD method1.
    SELECT * FROM zBarry_emp INTO exp WHERE empid = imp.
    ENDSELECT.
    IF sy-subrc NE 0.
      RAISE EVENT event1 .
    ENDIF.
  ENDMETHOD.

  METHOD interface~method2.
    SELECT * FROM zBarry_sal INTO exp2 WHERE empid = imp2.
    ENDSELECT.
  ENDMETHOD.

  METHOD eventhandler.
    WRITE:/ 'wrong empid'.
  ENDMETHOD.
ENDCLASS.               "class_test

Code Explanation

In the above code, we have done following implementation, step by step:

  1. Initially, we have defined Parameters to take Employee ID as input, variables to define work area to store data of Employee basic details and Salary details and object obj 
  2. Implementation of  one local class and an interface.
  3. Exporting Employee ID and getting relevant data from these classes/interface.
  4. Printing above result using Write statement.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.