References were introduced in ABAP in conjunction with ABAP Objects. A distinction is made between data references and object references. Object references are used as pointers objects. Object references (the content of object reference variables) are the only way to access the components of objects in an ABAP program. You can use references to access attributes and methods, but not events.
Object reference variables contain object references. An object reference variable oref is either initial or contains a reference to an existing object. In the latter case, the logical expression oref IS BOUND is true. An object reference variable that points to an object contains the 'address' of the object. Objects and their components can only be addressed through object references.
Reference variables are data objects with a deep data type. A reference variable can be defined as the smallest indivisible unit of complex data objects such as structures and internal tables. When handling reference variables or complex data objects that contain reference variables, note the special features that apply for deep types.
Object reference variables can be created either as class reference variables or interface reference variables.
As well as in declarations, you can also use the TYPE REF TO ... addition in all other statements in which type specifications are possible, for example, to specify the types of interface parameters in procedures.
Within a class there are two specific references exist in a class, the self-reference me and the pseudo reference super.
Like other variables, object reference variables are initialized using the statement CLEAR. The initial value of an object reference variable is a reference that does not point to any object.
Object references can be assigned between object reference variables. (using the MOVE statement). This means that the references in several references variables can point to the same object (sharing). If you make assignments between reference variables, the dynamic type of the source variable must be compatible with the static type of the target variable. The dynamic type is the class of the object to which the reference in a reference variable points. The static type is the type (class or interface) with which the reference variable has been typed. The static type of the target variable must always be more general than the dynamic type of the source variable.
The syntax check in the program can only compare the static type of the source variable with the static type of the target variable. If reference variables are assigned using the MOVE statement or the assignment operator (=), the system performs this comparison and assignments can only be made that fulfill the above requirements for static types (up cast). The same applies when passing parameters to procedures. Assignments are always possible if the static type of the target variable can address the same number of components as the static type of the source variable or fewer components:
If cref1 and cref2
are class reference variables, and iref1 and
iref2 are interface reference variables, the following assignments can be checked statically:
object is a predefined empty class and the root node for all inheritance trees in ABAP Objects. This class has no components and is the generic type of all object reference variables.> has for normal variables. Reference variables with the type object can be used as containers for passing references. They cannot be used for static access to objects. Dynamic access is possible.
If a static type check is not possible, or whenever you want the type check to take place at runtime, you need to use the MOVE ... ?TO statement or the casting operator (?=). When you use either of these options, there is no static type check. Instead, the system checks at runtime whether the object reference in the source variable points to an object to which the object reference in the target variable may also point (down cast). The dynamic type of the source variable is compared with the static type of the target variable. If assignment is possible, the system assigns the reference, otherwise, it generates the treatable exception CX_SY_MOVE_CAST_ERROR.
The syntax requires you to use a down cast in all cases that are not listed above for static type checks.
You can pass object references as actual parameters to procedures (methods, function modules, and subroutines) by specifying object reference variables. As far as typed formal parameters are concerned, note that you may only pass actual parameters of exactly the same type if the formal parameter can be changed in the procedure. This applies also to EXPORTING and CHANGING parameters of function modules and methods, parameters in subroutines passed by reference and CHANGING parameters in subroutines passed by value.
Passing actual parameters to formal parameters that corresponds to an up cast, such as passing a class reference variable to a formal parameter which is typed with reference to an interface of the class or one of its superclasses, is not possible if the parameter can be changed in the procedure.
The reason is that a reference could be assigned to the actual parameter in the procedure which is incompatible with its static type, such as a reference to any other class that implements the same interface or a reference to a subclass in another path of the inheritance tree.
When you assign object references to typed field symbols, the same applies as when you pass a reference to typed formal parameters: The types must be identical. Otherwise, incompatibilities between dynamic and static types may occur. For example, if you assign two class reference variables of different classes implementing the same interface one after the other to the same field symbol, an inconsistent state results.