Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  ABAP Objects - Overview →  Objects 

Object References

Object references are used as pointer objects. Object references (the content of object reference variables) are the only way to access the components of objects in an ABAP program. References can be used to access attributes and methods, but not events.

Other versions: 7.31 | 7.40 | 7.54

Object Reference Variables

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 predicate 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.

Static Type of Object Reference Variables

Object reference variables can be created either as class reference variables or interface reference variables.

Class Reference Variables

A reference variable with the static type of a class is called a class reference variable. Class reference variables are defined by the addition TYPE REF TO class of the TYPES statement or the DATA statement. In this case, class is a class known at the point of declaration.
A class reference variable cref enables the user to use the form cref->comp to access all of the visible components comp of the object to which the object reference points, which usually means all public components of the class.

Interface Reference Variables

A reference variable with the static type of an interface is called an interface reference variable. Interface reference variables are defined by the addition TYPE REF TO intf of the TYPES statement or the DATA statement. In this case, intf is an interface known at the point of declaration.
An interface reference iref enables the user to use iref->icomp to access all of the visible interface components icomp of the object to which the reference points. Interface components are those components of an object that are declared by implementing the interface in the class.

As well as in declarations, the addition TYPE REF TO ... can be used in all other statements in which types can be specified, for example to specify the types of interface parameters in procedures.

Special Object Reference Variables

Within a class there are two specific references, the self-reference me and the pseudo reference super.

  • The self-reference me is used to explicitly address an object's components. In the instance methods of a class, me is a predefined local reference variable with the same type as the class in which the instance method is implemented, which points to its own instance. It can be specified when addressing its own component but does not have to be. It can also be assigned to other reference variables or passed to methods.
  • The pseudo reference super is used to call the hidden method in a redefined instance method during inheritance. The pseudo reference reference is not a genuine reference because an instance of a subclass cannot have an associated superclass instance, and hence cannot have a reference variable. The pseudo reference super is the syntactical expression for a method call in a superclass.

Initializes Object Reference Variables

Like other variables, object reference variables are also initialized using the statement CLEAR. The initial value of an object reference variable is a reference that does not point to any object.

Assigning Object References Using Up Cast

Object references can be assigned between object reference variables. This means that the references in several reference variables can point to the same object (sharing). If assignments are made 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 assignment operator (=), the system performs this comparison and assignments can only be made that meet the above requirements for static types (up cast). The same applies when passing parameters to procedures. Assignments of this kind 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:

  • cref1 = cref2
Both class reference variables must refer to the same class, or the class of cref1 is a superclass of the class of cref2. The class of cref1 can therefore always be object, because object is the superclass for all classes in ABAP Objects.
  • iref1 = iref2
Both interface reference variables must refer to the same interface, or the interface of iref2 must be a nested interface that contains interface of iref1 as a component.
  • iref1 = cref1
The class of the class reference variable cref1 or one of its superclasses must implement the interface of the interface reference variable iref1. The implementation can be performed directly or using a nested interface that contains the interface of iref1 as a component.
  • cref1 = iref1
The class of cref1 must be object, that is, the reference variable cref1 must have the type REF TO object.

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. 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.

Assigning Object References Using Down Cast

In all cases where a static type check is not possible, and in all cases where type checks are not performed until program runtime, an explicit down cast is necessary. The following make this possible:

  • Assignments with the casting operator ?=. In this case, 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. 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 raises the handleable exception CX_SY_MOVE_CAST_ERROR.
  • Constructor expressions using the casting operator CAST, which can be used in all operand positions. This has the same effect as an assignment, but can reduce the number of helper variables needed.

The syntax requires a down cast to be used in all cases that are not listed above for static type checks.


Example

cref1 ?= iref1
Assigning an interface reference variable to a class reference variable. For the assignment to be successful, the object to which iref1 points must belong to the same class as the type of the class reference variable cref1 or to an appropriate subclass.

Object References as Actual Parameters

Object references can be passed as actual parameters to procedures (methods, function modules, and subroutines) by specifying object reference variables. As far as typed formal parameters are concerned, only actual parameters of exactly the same type can be passed if the formal parameter can be modified 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.

Assigning Object References to Field symbols

When object references are assigned to typed field symbols, the same applies as when passing a reference to typed formal parameters: The types must be identical. Otherwise, incompatibilities between dynamic and static types may occur. For example, if two class reference variables of different classes implementing the same interface one after the other are assigned to the same field symbol, an inconsistent state is produced.