abap news
Home » ABAP IS BOUND vs. IS INSTANCE OF Before a Downcast

ABAP IS BOUND vs. IS INSTANCE OF Before a Downcast

ABAPBy xabap Published
Navy shield with two blue check marks representing object existence and type compatibility.

Use IS BOUND to check that an object reference points to an object, and IS INSTANCE OF to check its type compatibility. Before downcasting and accessing subclass members, combine both checks: a bound reference can have the wrong type, while an initial reference can still satisfy IS INSTANCE OF.

Existence and type answer different questions

For object references, IS BOUND establishes that an object exists. It says nothing about whether that object belongs to the subclass you need. SAP defines this distinction in its documentation for [IS BOUND](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenlogexp_bound.htm).

For a populated reference, IS INSTANCE OF examines the dynamic type. For an initial reference, it examines the static type instead. That second rule explains why a successful type check alone does not justify accessing an attribute. See SAP’s [IS INSTANCE OF rules](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenlogexp_instance_of.htm).

Compare three references in one executable example

This example targets Standard ABAP 7.50 or later; the linked 7.50 documentation confirms the predicates and casting syntax. Its executable REPORT wrapper and list output are for Standard ABAP, not ABAP Cloud.

Create executable program ZREF_CHECKS. The local classes supply all types and sample data; no database objects or SAP demo classes are required. The assertions record the expected comparison results.

REPORT zref_checks.

CLASS lcl_document DEFINITION.
ENDCLASS.
CLASS lcl_document IMPLEMENTATION.
ENDCLASS.

CLASS lcl_invoice DEFINITION
  INHERITING FROM lcl_document.
  PUBLIC SECTION.
    DATA amount TYPE i VALUE 42.
ENDCLASS.
CLASS lcl_invoice IMPLEMENTATION.
ENDCLASS.

DATA empty_invoice TYPE REF TO lcl_invoice.
DATA plain_document TYPE REF TO lcl_document.
DATA invoice_document TYPE REF TO lcl_document.
DATA narrowed TYPE REF TO lcl_invoice.

START-OF-SELECTION.
  plain_document = NEW lcl_document( ).
  invoice_document = NEW lcl_invoice( ).

  ASSERT empty_invoice IS NOT BOUND.
  ASSERT empty_invoice IS INSTANCE OF lcl_invoice.

  ASSERT plain_document IS BOUND.
  ASSERT plain_document IS NOT INSTANCE OF lcl_invoice.

  ASSERT invoice_document IS BOUND.
  ASSERT invoice_document IS INSTANCE OF lcl_invoice.

  IF invoice_document IS BOUND
     AND invoice_document IS INSTANCE OF lcl_invoice.
    narrowed = CAST lcl_invoice( invoice_document ).
    WRITE / |Amount: { narrowed->amount }|.
  ENDIF.

Derive the expected results

empty_invoice receives no object, so it is unbound. Its declared type is already lcl_invoice, which makes the type predicate true.

plain_document points to a newly created base-class object. It is bound, but that object is not an invoice. invoice_document also has the base-class static type, but its actual object is an invoice, so both predicates succeed.

The guarded cast therefore reaches the invoice’s attribute, initialized to 42. The expected list output is Amount: 42; all six assertions should pass. These results are derived from the code, not an execution.

ReferenceIS BOUNDIS INSTANCE OF lcl_invoice
empty_invoiceFalseTrue
plain_documentTrueFalse
invoice_documentTrueTrue

Mistakes and when to avoid the downcast

Checking only IS BOUND would admit the base-class object. Casting that object to lcl_invoice would raise CX_SY_MOVE_CAST_ERROR. Also, CAST changes the reference’s static type; it does not create an invoice or convert the existing object. SAP describes these semantics in the [CAST documentation](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenconstructor_expression_cast.htm).

Do not add a type check when the reference is already declared with the required type: an existence check is sufficient before member access. If every caller repeatedly branches on subclasses, consider exposing the required operation through a shared interface or superclass method instead.

Sources: SAP ABAP 7.50: IS INSTANCE OF and initial references · SAP ABAP 7.50: IS BOUND · SAP ABAP 7.50: CAST operator