08.local Classes With Methods
08.local Classes With Methods
Local class with methods in SAP ABAP, defining methods in local classes, implementing
methods in SAP local classes
+ -
Local class is a class definition and implementation is available in a program.
Go to SE38, create a program ZSAPN_CLASS_METHODS1 and follow steps to add code
Define a class
IMPLEMENTATION
PUBLIC SECTION.
METHODS : GET_MATERAIAL_DETAILS
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
ENDCLASS.
Implement class
METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
Using class
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MAKTL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
DEFINITION DEFERED is a kwyword which indicates the class definition is delayed or postponed or
definition at some place in program.
REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_METHODS_EXAMPLE DEFINITION DEFERRED.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
Learner Questions
In definition part of the static method get_material_description you have set the exporting
parameter as ex_mara type mara and in implementation part it is reading data from MAKT
(select * from makt into ex_makt) where as it has not even defined.how would it will be
possible to implement?? and one more thing that in class method definition part before
public section declaration you have mentioned implementation ....I did not get that thing
particularly? help me on this regards. Thank You,
In definition part of the static method get_material_description you have set the exporting
parameter as ex_mara type mara and in implementation part it is reading data from MAKT
(select * from makt into ex_makt) where as it has not even defined.how would it will be
possible to implement?? and one more thing that in class method definition part before
public section declaration you have mentioned implementation ....I did not get that thing
particularly? help me on this regards. Thank You,
Using tables in SAP ABAP local classes, se11 table types in SAP Local classes
+ -
The bwlow example explains you of using table in SAP local classes using SAP ABAP programming
language.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT * FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.
Learner Questions
No Questions by learners, be first one to ask ..!!
Defining a local class with user defined types in SAP ABAP programming, local
classes with user defined table types
+ -
It is very important for us to use user-defined types when using local classes in SAP ABAP
programs, the below example explains using user-defined types in SAP ABAP programs.
The below example explains you of using user defined types in SAP Local classes in SAP ABAP
programming.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT MATNR MTART MEINS MATKL FROM MARA
INTO TABLE ET_MARA
UP TO 50 ROWS
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.
Learner Questions