Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Reference Variables 

CAST - Casting Operator

Other versions: 7.31 | 7.40 | 7.54


... CAST type( [let_exp] dobj ) ...


A constructor expression with the casting operator CAST performs a down cast or an up cast for the argument dobj and creates a reference variable of the static type type as a result. type can be specified as:

  • any non-generic data type dtype or the fully generic data type data
  • any object type (class or interface) including the fully generic object type object.
  • The # character as a symbol for the operand type. Can be specified only if the data type required in an operand position is unique and fully identifiable.

The parentheses must contain precisely one unnamed argument dobj. If a data type is specified, dobj must be a data reference variable. If an object type is used, dobj must be an object reference variable. dobj is a general expression position.

Casts of dobj to the specified type are performed in accordance with the same rules as apply to the casting operator for assignments, ?=. If the specified type type is more specific than the static type of dobj, it is a down cast. If type is more general than the static type of dobj, it is an explicit up cast.

A single expression not followed by the object component selector -> can be specified as a standalone statement. This makes it possible to test the feasibility of a down cast by catching the associated exception CX_SY_MOVE_CAST_ERROR.

If a single expression points to one data object or component of a data object using precisely one following object component selector, ->,

  • it can be specified as a writable expression in a result position. The type of the result must be convertible to the data type type with the following restriction: If the result is the result of a bit expression, type must be byte-like or character-like with the type c or string.

An optional LET expression let_exp can be specified before the argument to define local helper fields.


  • The casting operator CAST is suitable for avoiding the declaration of helper variables needed only for down casts.

  • Explicit up casts with the casting operator CAST are suitable for declaring a more general type of a declared reference variable in inline declarations.

  • No empty parentheses can be specified after CAST.

  • Unlike NEW, CAST works with an existing reference variable, which means that the result does not need to be saved to persist the referenced object.

  • The predicate expression IS INSTANCE OF or the case distinction CASE TYPE OF can be used to check whether a down cast can be applied to specific classes or interfaces.

  • Down casts are also possible using the INTO addition of the statement WHEN TYPE of a case distinction using CASE TYPE OF.


The source code section below shows two equally valid down casts with the two possible casting operators ?= and CAST. Before the actual cast, its feasibility is checked using the predicate expression IS INSTANCE OF.


DATA: oref1 TYPE REF TO c1, 
      oref2 TYPE REF TO c2. 

oref1 = new c2( ). 

IF oref1 IS INSTANCE OF c2. 
  oref2 ?= oref1. 
  oref2 =  CAST #( oref1 ). 


Uses dereferencing operator and component selector in CAST with data types. The constructor expressions can be used as writable expressions on the right and left of assignments with the assignment operator=.

TYPES: BEGIN OF t_struc, 
        col1 TYPE i, 
        col2 TYPE i, 
       END OF t_struc. 

DATA dref  TYPE REF TO data. 
DATA struc TYPE t_struc. 

dref = NEW t_struc( ). 

struc      = CAST t_struc( dref )->*. 
struc-col1 = CAST t_struc( dref )->col1. 
struc-col2 = CAST t_struc( dref )->col2. 

CAST t_struc( dref )->*    = struc. 
CAST t_struc( dref )->col1 = struc-col1. 
CAST t_struc( dref )->col2 = struc-col2.


RTTS often requires a helper variable to perform a down cast of a type description object to a specialized class. These examples show how helper variables can be omitted by using the operator CAST.

DATA(attributes) = 
  CAST cl_abap_classdescr( 
       cl_abap_classdescr=>describe_by_name( 'CL_ABAP_FORMAT' ) 

DATA(components) = 
  CAST cl_abap_structdescr( 
       cl_abap_typedescr=>describe_by_name( 'T100' ) 

DATA(no_of_components) = 
  lines( CAST cl_abap_structdescr( 
             cl_abap_typedescr=>describe_by_name( 'SYST' ) 
              )->get_components( ) ).


The factory method of the following class returns a reference variable of the type of the class itself. To use interface variables to access the components declared in the interface if, as recommended, the variable iref, declared inline, is cast to the type of the interface.


    CLASS-METHODS factory RETURNING value(ref) TYPE REF TO cl. 

  METHOD factory. 
    ref = NEW #( ). 

  DATA(iref) = CAST if( cl=>factory( ) ).