Preface – This post is part of the ABAP Programs series.
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.
Table of Contents
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 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:
- 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
- Implementation of one local class and an interface.
- Exporting Employee ID and getting relevant data from these classes/interface.
- Printing above result using Write statement.
0 Comments