TYPES - REF TO

Syntax

TYPES dtype { {TYPE REF TO type}
            | {LIKE REF TO dobj} }.

Effect

The addition REF TO specifies a data type for a reference variable. The entry behind REF TO specifies the static type of the reference variable. The static type restricts the object quantity to which a reference variable can refer. The dynamic type of a reference variable is the data type and the object class to which it refers. The static type is always more general or the same as the dynamic type (see also Assignments Between Reference Variables).

You can use the TYPE addition to define data types for data and object reference variables. You can only define data types for data reference variables with the LIKE addition.

Types for Data Reference Variables

If, for type, you have specified the predefined generic data type data, the system creates a data type for a data reference variable from the static type data. Such reference variables can refer to any data objects but can only be dereferenced in the statement ASSIGN.

If, for type, you have specified any non-generic data type (a non-generic data type from the ABAP Dictionary or from the public visibility section of a global class), a non-generic program-local type already defined with TYPES, or a non-generic predefined type (d, f, i, string, t, xstring), the system creates a data type for a data reference variable with the relevant static type. Such reference variables can refer to all data objects of the same type and can be dereferenced to matching operand positions using the dereferencing operator ->*.

If, for dobj, you have specified a data object that is visible at this point, the system creates a data type for a data reference variable whose static type is adopted from the data type of the data object. Such reference variables can refer to all data objects of the same type and can be dereferenced to matching operand positions using the dereferencing operator ->*. Within a procedure, you cannot specify a generic typed formal parameter for dobj.

Types for Object Reference Variables

If, for type, you have specified a global or local class, the system creates a data type for a class reference variable whose static type is the specified class. Such reference variables can refer to all instances of the class and its subclasses. You can access all components of an object with a class reference variable. If the static type is the super class of the dynamic type, then the components that can be statically called are a subset of the components of the dynamic type. You can call all attributes and methods of a dynamic type using a class reference variable and the dynamic object attribute call (see ASSIGN) and the dynamic method call (see CALL METHOD).

If, for type, you have specified a global or local interface, the system creates a date type for an interface reference variable whose static type is the specified class. Such reference variables can refer to objects of all classes that implement the interface. When calling components of objects with interface references, the name of the static type is always implicitly placed before the component. With an interface reference variable, you can only access the interface components of an object that are known in the static type. This applies for the dynamic and the static call.

Example

This example shows how data types are defined for an interface reference variable and for a class reference variable as well as a data reference to an interface reference variable.

INTERFACE i1.
...
ENDINTERFACE.

CLASS c1 DEFINITION.
  PUBLIC SECTION.
    INTERFACES i1.
ENDCLASS.

TYPES: iref TYPE REF TO i1,
       cref TYPE REF TO c1,
       dref TYPE REF TO iref.