ABAP - Keyword Documentation →  ABAP - Reference →  Program Flow Logic →  Expressions and Functions for Conditions →  log_exp - Logical Expressions →  rel_exp - Predicates →  rel_exp - Predicate Expressions → 

rel_exp - IS INSTANCE OF

Syntax

... oref IS [NOT] INSTANCE OF class|intf

Effect

The predicate expression IS INSTANCE OF checks whether

The comparison type must be an object type, meaning a class specified using class or an interface specified using intf that can be used in this place. oref expects an object reference variable with the static type of a class or of an interface. oref is a general expression position.

The expression is true in the following cases or false if NOT is used:

If a field symbol or a formal parameter is specified for oref, its type can be fully generic, which means it can be typed with any and it must be an object reference variable at runtime. If a generically typed field symbol or a generically typed formal parameter is specified for oref, the static type of the object reference variable is used that is represented by the field symbol or formal parameter at runtime.

A syntax error occurs if it is possible to identify statically that the result of the expression is false.

Notes

Example

All the following predicate expressions are true, since the dynamic type of the object reference variable oref is more specific or equal to the specified object type. See also Predicate Expression IS INSTANCE OF for a detailed example.

INTERFACE intf.
ENDINTERFACE.

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

CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.

DATA oref TYPE REF TO object.

oref = NEW c1( ).
ASSERT oref IS INSTANCE OF c1.
ASSERT oref IS INSTANCE OF intf.

oref = NEW c2( ).
ASSERT oref IS INSTANCE OF c2.
ASSERT oref IS INSTANCE OF c1.
ASSERT oref IS INSTANCE OF intf.

Example

The following example shows how the predicate expression IS INSTANCE OF can be used to check the feasibility of a down cast. This makes the exception handler (also shown) superfluous.

CLASS c1 DEFINITION.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.

DATA: ref1 TYPE REF TO c1,
      ref2 TYPE REF TO c2.

ref1 = NEW c2( ).

IF ref1 IS INSTANCE OF c2.
  ref2 ?= ref1.
ENDIF.

TRY.
    ref2 ?= ref1.
  CATCH cx_sy_move_cast_error.
ENDTRY.



Continue
Predicate Expression IS INSTANCE OF