05.interfaces in SAP Classes
05.interfaces in SAP Classes
Interfaces in SAP Classes, using interface concept with SAP Object Oriented
ABAP programming.
+ -
Before going to interfaces, we must know about Polymorphism .
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
+ -
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).
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
Save, double click on each method and create your own implementation.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare 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
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
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