0% found this document useful (0 votes)
96 views

05.interfaces in SAP Classes

Interfaces in SAP classes allow for polymorphism by defining common method names that can have different implementations in different classes. An interface contains method signatures without implementations, while a class implements an interface and provides the method bodies. Interfaces promote reusability and help maintain a standard framework. They are defined in SE24 and can then be implemented by multiple classes to extend their functionality using the common method names defined in the interface. Classes can also define aliases to provide alternate names for interface methods.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

05.interfaces in SAP Classes

Interfaces in SAP classes allow for polymorphism by defining common method names that can have different implementations in different classes. An interface contains method signatures without implementations, while a class implements an interface and provides the method bodies. Interfaces promote reusability and help maintain a standard framework. They are defined in SE24 and can then be implemented by multiple classes to extend their functionality using the common method names defined in the interface. Classes can also define aliases to provide alternate names for interface methods.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

# Content

1 Interface Concept in Object Oriented ABAP

2 Creating Class Interface in SAP Classes

3 Using aliases for interface methods in SAP Classes

Interface Concept in Object


Oriented ABAP
Last Updated: November 8th 2013 by Ashok Kumar Reddy

Interfaces in SAP Classes, using interface concept with SAP Object Oriented
ABAP programming.

+ -
Before going to interfaces, we must know about Polymorphism .

What is Polymorphism in Object Oriented Programming model ?


It is a concept by which the same method names will behave differently in different classes i.e each
method will have its own implementation in different different classes but with the same name.
Interface is one of the concept in Object Oriented ABAP to achieve Polymorphism.

What is an interface in Object Oriented ABAP ?

 Interfaces are independent structure which are used in a class to extend the functionality of a
class.
 Interfaces contains methods without any implementation. Where as a class contains
methods with implementation.
 We need to define an interface with the required method names in SE24 TCODE.
 The interfaces can be used by ‘n’ no of classes to extend the functionality
of the class.
 To implement an interface in a class just give the name of interface under the Ã
¢â‚¬Ëœinterface’ tab.
 All the interface methods will be automatically copied to the classes in a particular class
without effecting the other classes.
 The main use of interfaces is re-usability and maintain standard project framework.

In the above diagram, application is business application, Class A, Class B, Class C, Class D and
Class E are independent classes, all these classes are using one Interface called Interface( in
diagram).

In all the classes,the methods names are same but the implementation will be different from one
class to another class thereby achieving the concept called POLYMORPHISM.Polymorphism is
implemented in the from of interface.

Learner Questions

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

Creating Class Interface in SAP


Classes
Last Updated: November 12th 2013 by Ashok Kumar Reddy

Creating an interface in SAP classes

+ -
As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all the
custom interfaces starts with ZIF_ , in the below example we will be learning how to create a use
interfaces in SAP.

All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with ZIF_,
based on the provided name (at the time of creation), SAP will automatically determine and creates
(interface or class).

Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.

Provide description, save, save it in a local object.

Go to methods tab, add method name as GET_MATERIAL_DETAILS-INSTANCE, click on


parameters button and add below parameters.
Save, Go back to methods and add one more method GET_MATERIAL_DESCRIPTIONS.

Click on parameters and provide parameters as below.

Save and activate.

We have created an interface with two methods (which dosen`t have implementation).
No Go to SE24 and create a class ZCL_SAPN_CLASS_EX

Provide description, save it in a local object.

Go to interfaces tab and add interface name as ZIF_SAPN_MATERIAL_EXAMPLE, enter


Now go to methods tab, you can see all the methods in the interface are automatically copied.

Save, double click on each method and create your own implementation.

Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and add below


code.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add below


code

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = 'E'. "English description only
ENDSELECT.

Save and activate the class.

Using class in program


Create a SE38 program and follow the steps below. The interface method will be clalled by
usingINTERFACENAME~METHODNAME.

REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-
MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX, WA_MAKT-
SPRAS.
Similarly create another class and repeat the same process, we can add interfaces into multiple
classes.

Learner Questions

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

Using aliases for interface


methods in SAP Classes
Last Updated: November 12th 2013 by Ashok Kumar Reddy

Using aliases concept in SAP classes, provide alternate names for interface
methods in SAP classes

+ -
Aliases is a concept of providing alternate method names for interface methods in implemented
class(where interface is implemented).
Whenever we implement an interface in a class, the method will be copied with
INTERFACENAME~METHODNAME, see below example.

By using aliases concept we can provide alternate name for interface methods and we can call
methods with that name, to add alternate names click on aliases tab and provide altternate names.
Calling alias method is same as normal method CALL METHOD OBJECT->ALIASMETHDO
NAME, see below example.

Please refer previous lesson Using interface class in SAP Classes for better understanding.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->GET_MATERIAL_DESCRIPTIONS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-
MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX, WA_MAKT-
SPRAS.

Learner Questions

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

You might also like