0% found this document useful (0 votes)
393 views5 pages

11.interfaces in Local Classes in SAP ABAP

This document discusses creating and using interfaces in local classes in SAP ABAP. It provides the following steps: 1. Define an interface with methods but no implementation. 2. Define a local class and implement the interface. 3. Implement the interface methods in the local class. 4. Create an object of the local class and call the interface methods to retrieve material details and descriptions.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
393 views5 pages

11.interfaces in Local Classes in SAP ABAP

This document discusses creating and using interfaces in local classes in SAP ABAP. It provides the following steps: 1. Define an interface with methods but no implementation. 2. Define a local class and implement the interface. 3. Implement the interface methods in the local class. 4. Create an object of the local class and call the interface methods to retrieve material details and descriptions.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

# Content

1 Creating and Using interfaces in Local Classes in SAP

Creating and Using interfaces in


Local Classes in SAP
Last Updated: November 14th 2013 by Ashok Kumar Reddy

Creating a class interface locally and using it in a local class program - SAP
ABAP programming

+ -
The below example explains you using interface concept in SAP ABAP local classes, most of the
times we don`t prefer interfaces in local classes, any how we will learn how to use them in local
calsses.

The below syntax is used to declare interface in local classes.


INTERFACE <INTERFACE NAME> .
**Define methods and events
ENDINTERFACE.

To implement the local interface in a local class, we use below syntax.


CLASS <CLASS NAME> DEFINITION.
PUBLIC SECTION. "visibility
INTERFACES <INTERFACE NAME> .
ENDCLASS.

In the below example we are creating and using interfaces locally, please go through the example
of creating and using interface in global classes , the same we are doing in a local class.

Follow the below steps to create and use interfaces in local classes.

Define an interface
Define an interface and add methods which dosen`t need any implementation.
INTERFACE CL_INTERFACE.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
METHODS : GET_MATERIAL_DESCRIPTIONS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MAKT TYPE MAKT.
ENDINTERFACE.

Define a class and add interface


Define a local class and add the above interface into it .
CLASS CL_CLASS DEFINITION.
PUBLIC SECTION.
INTERFACES CL_INTERFACE.
ENDCLASS.

Implement the methods in local class


Implement the methods of interface in the defined local class.
CLASS CL_CLASS IMPLEMENTATION.

METHOD CL_INTERFACE~GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDMETHOD.
METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
SELECT * FROM MAKT
INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
ENDSELECT.

ENDMETHOD.

ENDCLASS. "cl_clsss
Creat object and use it in the program
Now the class is created and implemented, we can use the call methods .
DATA LO_CLASS TYPE REF TO CL_CLASS.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
START-OF-SELECTION.
CREATE OBJECT LO_CLASS.

CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

The final program source will be

REPORT ZSAPN_LOCAL_CLASS_INTERFACE.

CLASS CL_CLASS DEFINITION DEFERRED.


DATA LO_CLASS TYPE REF TO CL_CLASS.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
INTERFACE CL_INTERFACE.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
METHODS : GET_MATERIAL_DESCRIPTIONS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MAKT TYPE MAKT.
ENDINTERFACE.

CLASS CL_CLASS DEFINITION.


PUBLIC SECTION.
INTERFACES CL_INTERFACE.
ENDCLASS.

START-OF-SELECTION.
CREATE OBJECT LO_CLASS.

CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.


CLASS CL_CLASS IMPLEMENTATION.
METHOD CL_INTERFACE~GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDMETHOD.
METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
SELECT * FROM MAKT
INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
ENDSELECT.

ENDMETHOD.

ENDCLASS. "cl_clsss

Learner Questions

No Questions by learners, be first one to ask ..!!

You might also like