Monday, February 7, 2011

Working with interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part, and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

  • Interfaces are listed in the definition part of the class, and must always be in the PUBLIC SECTION.

  • Operations defined in the interface are implemented as methods of the class. All methods of the interface must be present in the implementation part of the class.

  • Attributes, events, constants and types defined in the interface are automatically available to the class carrying out the implementation.

  • Interface components are addressed in the class by ~

Go to SE24 provide interface name.

Interface name start with ZIF_

Press create button.

Provide description.

Save it.

Save it.

Provide the method name.

Provide the parameters for this method.

The screen is like this.

Then save it, check it, activate it.

We cannot implement the method of this interface.

Provide the name in the class.

Create it.

Save it.

Go to interface tab.

Provide the interface name.

Save it.

Then go to the methods tab.

Then we can see the interface method name in the class method.

Then double click on the method then write the logic here.

Then save it, check it, activate it.

Create a program in SE38.

Provide the code.

*&---------------------------------------------------*
*& Report ZCL_INTERFACE *
*&---------------------------------------------------*
REPORT  ZCL_INTERFACE .
*provide mara table
DATA: MARA TYPE MARA.
*provide data objects
DATA: OBJ TYPE REF TO ZCL_INTERFACE,
IT_MARA TYPE Z_MARA,
WA_MARA TYPE MARA.
*provide selection screen
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
*provide object
START-OF-SELECTION.
CREATE OBJECT OBJ.
*call the method.
CALL METHOD OBJ->ZIF_INTERFACE~SELECT_METHOD
EXPORTING
P_MATNR_LOW = S_MATNR-LOW
P_MATNR_HIGH = S_MATNR-HIGH
IMPORTING
IT_MARA = IT_MARA
WA_MARA = WA_MARA.
*display the data
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR,
WA_MARA-ERSDA,
WA_MARA-ERNAM,
WA_MARA-MATKL,
WA_MARA-MEINS.
ENDLOOP.

Then save it, check it ,activate it then execute it the output is like this.

The output is see in the list.

What is the use of aliases.

ALIASES:

This is the aliases name. it is only for interfaces.

Go to se24.

Then go to aliases tab.

Then provide another name for the interface method.

Then provide public.

Save it, check it, activate it.

Then go to SE38.

Change the method name also.

*&---------------------------------------------------------------------*
*& Report ZCL_INTERFACE *
*& *
*&---------------------------------------------------------------------*
REPORT  ZCL_INTERFACE .
*provide mara table
DATA: MARA TYPE MARA.
*provide data objects
DATA: OBJ TYPE REF TO ZCL_INTERFACE,
IT_MARA TYPE Z_MARA,
WA_MARA TYPE MARA.
*provide selection screen
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
*provide object
START-OF-SELECTION.
CREATE OBJECT OBJ.
*call the method.
* CALL METHOD OBJ->ZIF_INTERFACE~SELECT_METHOD
CALL METHOD OBJ->SEL
EXPORTING
P_MATNR_LOW = S_MATNR-LOW
P_MATNR_HIGH = S_MATNR-HIGH
IMPORTING
IT_MARA = IT_MARA
WA_MARA = WA_MARA.
*display the data
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR,
WA_MARA-ERSDA,
WA_MARA-ERNAM,
WA_MARA-MATKL,
WA_MARA-MEINS.
ENDLOOP.

The output would be as shown below:.





No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites