Local Classes in SAP ABAP

Introduction

One of the flavors of ABAP classes (Global Classes) we have already discussed in the last article. Another flavor of ABAP class are Local Classes. The only difference between a local class & global class is visibility & accessibility. A local class can be accessed within that program only in which it is being defined & implemented, whereas a global class can be accessed in any other program.

NOTE: In some of the previous articles, where different types of classes have been described, there the sample programs were the examples of local class only.

Definition

A class which is defined and implemented in a program is known as Local Class. Local classes in SAP ABAP can be created through Transaction code SE-38(tool to create a report).

Program

Let’s take an example of this class ‘ZCL_MARA’. This class is having one method ‘GET_MATERIAL_TYPE’  to get the material type of a given material.

NOTE: ‘MARA’ is a table in SAP ABAP for general material data. So, in this sample program, material (iv_matnr – importing parameter of a method) is given by the user, and this method will fetch the material type ( like finished, semi-finished, raw, etc. )

TYPES: BEGIN OF ty_mara,                                       “USER-DEFINED TYPES

Matnr TYPE mara-matnr,

Mtart TYPE mara-mtart,

END OF ty_mara.

CLASS zcl_mara DEFINITION.

PUBLIC SECTION.

METHODS: get_material_type

IMPORTING iv_matnr TYPE mara-matnr

EXPORTING ls_mara type ty_mara.

ENDCLASS.                                                                    “CLASS DEFINITION

 

CLASS zcl_mara IMPLEMENTATION.

METHOD get_material_type.

SELECT SINGLE matnr, mtart FROM mara INTO ls_mara

WHERE matnr = iv_matnr

ENDMETHOD.

ENDCLASS.                                                                   “CLASS IMPLEMENTATION

 

Advantages

  1. To restrict the usage of any functionality, we can use it in a local class of any program.
  2. A Local class is very useful while creating an ABAP Unit Test Class, as a local test class has been generated within a global class.
  3. These classes are useful for the enhancements of class methods.

Leave a comment

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