Downcasts with Interfaces
To assign an interface reference to a class reference where the class has implemented the interface, you must use the down-cast assignment operator MOVE ... ?TO ... or its short form ?=. Otherwise, the system would return a message stating that it is not certain that all components that can be accessed syntactically after the cast assignment are actually available in the instance. As a rule, the implementing class contains more components than the interface.
Interface reference variables can contain references to instances of the implementing class at runtime. After assigning this type of reference back to a reference to the implementing class, clients are no longer limited to interface components.
This type of reference variable assignment is described as down-casting. The down-casting view is widened or at least unchanged. This is a switch from a view of a few components to a view of more components. The term Narrowing Cast is also used.
A typical area of use for down-cast assignments is when specific components of instances need to be addressed whose references are kept in variables that are typed on the interface. A user who is interested in the finer points of the instances in implementing classes cannot use the interface reference because the downcast assignment only allows access to the interface components. In the example shown in the figure, a travel agency (LCL_TRAVEL_AGENCY) needs to book a flight but keeps all the various business partner references in an internal table that was typed on the interface LIF_PARTNER.
What happens if there is no airline reference in the interface reference GO_PARTNER at runtime, but the down-cast assignment operator is used to copy the reference to the then invalid reference GO_CARRIER? In contrast to the up-cast assignment, it is possible that the static type of the target variable (GO_CARRIER) is neither more general than nor the same as the dynamic type of the source variables (GO_PARTNER), specifically if GO_PARTNER contains hotel or car rental references.
The runtime system checks this type of cast before the assignment, whether or not the current content of the source variable corresponds to the type requirements of the target variable. Otherwise, an exception that can be handled is triggered, and the original value of the target variable remains the same. This exception of error class CX_SY_MOVE_CAST_ERROR can be identified using TRY-ENDTRY and the CATCH statement.
Another way to prevent this runtime error would be to use runtime type identification (RTTI) classes. They can be used to determine the dynamic type at runtime and to set a condition for the cast.
Assignments between interface reference variables, whose typing interfaces are not related to each other, cannot be checked statically and must, therefore, be performed using down-cast. With such an assignment, the system checks at runtime whether the class of the instance to which the source reference refers also supports the interface with which the target reference is typed.
Implementation of Interface Hierarchies
An interface implementation strongly resembles regular inheritance. With regular inheritance, you can define large hierarchies of classes. When we use interfaces instead of classes we call that interface hierarchies. We would like to illustrate this technique with an application example.
In this example, you need to know whether it is useful to define a further service for “room reservation” in the interface LIF_PARTNER. In this case, the classes LCL_CARRIER and LCL_RENTAL need to implement the appropriate method because they have integrated with the interface LIF_PARTNER. However, keeping the same semantics is not conceivable for airlines or car rental companies when we implement the "room reservation" service.
However, because there are several other business partner types for which this implementation is useful for example, motels and hotels, the method needs to be defined centrally and not individually for motels and hotels. If we also need to extend the model with other accommodation provider types, for example, guesthouses, then the interface hierarchy needs to be retained.
Definition and Implementation of Compound Interfaces – Syntax