Assignment Rules for Reference Variables

The content of a reference variable can only be assigned to another reference variable. Data references can only be assigned to data reference variables and object references can only be assigned to object reference variables. No conversion takes place in this type of assignment. In order for an assignment to take place, the static type of the target reference variable must be more general than or equal to the dynamic type of the source reference variable. After successful assignment, the target reference variable points to the same object as the source reference variable or the target reference variable adopts the dynamic type of the source reference variable.

Static and Dynamic Types

Each reference variable has a dynamic and a static type.

The static type of a reference variable is always less specific or the same as the dynamic type. This basic rule applies to all assignments between reference variables.

Up Cast and Down Cast

In an assignment between reference variables, the target variable adopts the dynamic type of the source variable. An assignment is possible if the static type of the target variable is less specific or the same as the dynamic type of the source variable.

Up Cast

If the static type of the target variable is less specific or the same as the static type of the source variable, assignment is always possible. The name up cast arises from the fact that you are moving upwards within the inheritance space. As the target variable can accept more dynamic types in comparison to the source variable, this assignment is also know as a widening cast. An up cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. This includes, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or the transfer from actual to formal parameters.

Down Cast

If the static type of the target variable is more specific than the static type of the source variable, you must check at runtime before the assignment is executed, whether it is less specific or the same as the dynamic type of the source variable. The name down cast arises from the fact that you are moving downwards in the inheritance space. Because the target variable can accept fewer dynamic types in comparison to the source variable, this assignment is also known as a narrowing cast. A down cast is only possible using the special assignment operator ?= (casting operator) or the statement MOVE ... ?TO .... If this prerequisite is not fulfilled, a treatable exception occurs.