An alias in general term is used to indicate the alternate name for a given person, or thing. Similarly, in OOPs ABAP, an alias is an alternate name given to the interface methods implemented in the class. Whenever we implement the interface in a class, the interface method is copied with naming convention <Interface-Name>~<Method-Name>. Instead of using the default interface method name in the class, we can provide an alternate name to it (called alias) and can use alias name in the program. In this article we will learn more about Alias in SAP Classes.
An alias is an alternate name for interface methods that are implemented in the class.
How to give alias name to interface methods in the SAP Classes?
In transaction SE24, provide the class name and open it in editable mode. There will be one Alias tab. Provide the alias name corresponding to the interface methods.
Calling interface methods in the class will be easy by the use of an alias.
CALL METHOD <object-name> -> <alias-interface-method>
The below syntax is used to give alias in the program:
ALIASES <alias-name> FOR <interface-method>.
Let us suppose an interface is implemented in the class and we have provided alias name to the interface method.
ZIF_MATERIAL~GET_MATERIAL_DETAILS (Interface Method) – DETAILS (Alias Name)
DATA : object_name TYPE REF TO zcl_demo.
DATA: wa_mara TYPE MARA. “MARA Declaration
PARAMETERS p_matnr TYPE MARA-MATNR. “User Input
CREATE OBJECT object_name. “Object creation
** CALL METHOD object_name -> ZIF_MATERIAL~GET_MATERIAL_DETAILS.**
CALL METHOD object_name -> details. “Method call with alias name
IM_MATNR = p_matnr
EX_MATNR = wa_mara.
The above code retrieves the material details based on the input material number (MATNR) from interface ZIF_MATERIAL. Here we have used the alias name DETAILS as method name for the simplicity of understanding the code. Instead of DETAILS, we can also use ZIF_MATERIAL~GET_MATERIAL_DETAILS, but using this more than once in tricky code will make the code much more complex.