MichaelB.
7/28/2018 - 11:51 AM

Define ALIASES for interface components in a class or compound interface

Use alias name to make it easier to refer to interface components from calling programs. You still have to use the full name in the class itself. You can assign alias names when:

  • Implementing interfaces in the declaration part of a Class
  • When an interface is composed during the interface definition itself

Place alias names inside the visibility section of the class. This determines how the alias name is seen outside of the class.

A benefit of using alias names - If a class definition or interface declaration changes but the alias name stays the same, a calling program is never affected (because to the calling program the interface of the class has stayed exactly the same) -> means we do not break other people's (calling) programs when we change our classes.

When we have a scenario where we nest i.e. 3 levels deep and we want to create an alias name in the upper level for the lowest level method, we have to create an alias for the method in the intermediate level also. Without doing that we wouldn't be able to use an alias in the upper level interface.

* In a class

ALIASES name FOR intf~co

*************************
*  In Compound Interfaces

INTERFACE intf_a.
    METHOD m1.
ENDINTERFACE.

INTERFACE intf_b.
    INTERFACES: intf_a.
    ALIASES am1 FOR intf_a~m1.
ENDINTERFACE.

INTERFACE intf_c.
    INTERFACES: intf_b.
    ALIASES am2 FOR intf_b~am1.
ENDINTERFACE.

CLASS aclass DEFINITION.
    PUBLIC SECTION. 
        INTERFACES intf_c.
ENDCLASS.

* Calling Program
...
DATA myclass TYPE REF TO aclass.
myclass->am2.

*************************
* Practical Example for aliases for interface components in a class

INTERFACE intf_speed.
  METHODS: writespeed.
ENDINTERFACE.

CLASS train DEFINITION.
  PUBLIC SECTION.
    INTERFACES intf_speed.
    ALIASES writespeed FOR intf_speed~writespeed.
    METHODS gofaster.
  PROTECTED SECTION.
    DATA: speed TYPE i.
ENDCLASS.

CLASS train IMPLEMENTATION.
  METHOD gofaster.
    speed = speed + 5.
  ENDMETHOD.
  METHOD intf_speed~writespeed.         "Still have to use the full name here
    WRITE: / 'The train Speed is: ', speed LEFT-JUSTIFIED.
  ENDMETHOD.
ENDCLASS.

* Our program starts here

DATA mytrain type REF TO train.

START-OF-SELECTION.

  CREATE OBJECT mytrain.
  mytrain->gofaster( ).
*  mytrain->intf_speed~writespeed( ).
  mytrain->writespeed( ).