SAP CLASS REF INTERF REF - Guide
Get Example source ABAP code based on a different SAP table
Class References and Interface References
ABAP_BACKGROUND
Interface components in objects can be addressed using a class reference variable or an interface reference variable. If you use a class reference variable, the interface component is addressed using the name of the interface and the interface component selector (
ABAP_RULE
From outside a class, only access its interface components using a relevant interface reference variable; do not use the interface c omponent selector (
ABAP_DETAILS
Accessing interface components externally using an interface reference variable makes code easier to understand because it is clear that the user of the class is interested in exactly the aspect provided by the interface. Accessing interface components using a class reference variable, on the other hand, suggests that components are used that are not provided by an interface. As a rule, only use the interface component selector within classes and interfaces, to address the interfaces included there. If you want to provide an interface component of an included interface as a separate component, you can declare an alias name by using
ABAP_EXAMPLE_BAD
The following source code shows an interface method call using a class reference variable and the interface component selector (~); this is not recommended, as mentioned in the rule above.
PUBLIC SECTION.
INTERFACES if_intf.
...
ENDCLASS.
...
DATA cref TYPE REF TO cl_class.
...
cref->if_intf~meth( ).
...>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code shows the method call from the above example, but using an interface reference variable. Instead of
PUBLIC SECTION.
INTERFACES if_intf.
...
ENDCLASS.
...
DATA iref TYPE REF TO if_intf.
...
iref->meth( ).
...>
ABAP_EXAMPLE_END