ABAP Objects: Widening Cast

By | September 14, 2008 | ABAP Objects, OO Concepts | 57,174 | 8

When we assign the instance of the Superclass to the Subclass, than it is called the Widening Cast, because we are moving to the “More Specific View” from the “Less specific view”.

Everytime it is not possible to move the Superclass reference to the Subclass, because subclass will(or might) have always more functionality compare to the Superclass. So, we need to be more careful when we use the down casting. We will use the same ANIMAL Superclass and LION subclass as per the blog post – ABAP Objects: Narrowing Cast

When this Widening Cast is used?

Widening cast can be used when we need to access the Specific functionality of the subclass from the Superclass. In ABAP, it is necessary to catch the exception CX_SY_MOVE_CAST_ERROR while doing the widening cast to avoid the short-dump.

Code Lines

Check out the code lines for Widening Cast:

* This code shows how to use the Widening cast (downcasting)
  DATAlo_animal TYPE REF TO lcl_animal,
        lo_lion   TYPE REF TO lcl_lion,
        lo_cast_error TYPE REF TO cx_sy_move_cast_error.
* First We have to do Narrow casting in order to set the
*   Animal reference to LION reference.
  DATAlo_tmp_lion   TYPE REF TO lcl_lion.
  CREATE OBJECT lo_tmp_lion.
  lo_animal lo_tmp_lion.
* Now, we will do the Widening cast to move the reference from the
*   Animal to LION (more specific class).
      lo_lion ?= lo_animal.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE'Widening cast failed 1'.
  IF lo_lion IS NOT INITIAL.
    CALL METHOD lo_lion->hungry).
    CALL METHOD lo_lion->fasting).
* Now, we will try to do the widening cast without setting up
*   the proper object reference in the Super reference.
  CREATE OBJECT lo_animal.
      lo_lion ?= lo_animal.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE'Widening cast failed 2'.
  IF lo_lion IS NOT INITIAL.
    CALL METHOD lo_lion->hungry).
    CALL METHOD lo_lion->fasting).


Output of this code snippet is:

Important — Terms: Upcasting & Downcasting

I guess in older version SAP was using (only) Super Class to point out the super class. But from the release 711, they are referring it as the ROOT class.

This would change the view perspective from where you are seeing the inheritence tree.
For Super class, we always think as the top-down tree which has SUPER class as the top node and the subclass as the down node. In this scenario, Narrowing Cast would be the Upcasting as you are going from BOTTOM to UP.

If we have the tree as the DOWN-TOP where ROOT class would be at the DOWN and we will build our subclasses on top of that. So, that makes the Narrowing Cast as the Downcasting.

So, it is advisable to always consider NARROWING CAST or WIDENING CAST terms and not to go with the UP or DOWN casting.

Check out all ABAP Objects Concepts

Explore other ABAP Objects Concepts …

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • Anonymous

    Thanks for the example. But sorry I disagree with your use of the Terms.
    SAP post 6.40 now suggest that the unambiguous terms of UP and DOWN are used.
    SAP have swapped the terms NARROW and WIDE around and so prefer you to use UP and DOWN.

  • Anonymous

    very good example to understand the casting concepts of OOP ABAP.

  • can any1 tell me why the 2nd widening castr goes fail.
    plz mail me abap1942@gmail.com
    althought good explanation

  • Hi Naimesh Patil,
    I have confused what you have explained about Widenig-cast and Narrow-cast, because in BC401 material they have explained that wideing cast is If you assign a "subclass reference" to a "superclass reference" it is called Widening-cast but you explained that a "superclass reference" to a "subclass reference" so i am in confusion .
    please can you guide me what exactly Widening-cast and Narrow-cast?

  • Akkamahadevi,

    Yes it is confusing as BC401 was designed with 4.6C in mind, and this terminology was interchanged as of 6.40.

    This is the comment from Horst Keller:
    the solution is, that we changed the terminology for for narrowing and widening casts between Releases 6.40 and 7.00.

    This is documented as Modification 6 under http://help.sap.com/abapdocu/en/ABENNEWS-70-DOCU.htm#!ABAP_MODIFICATION_6@6@

    Naimesh Patel

  • Thank You Naimesh…
    But that link is not opening, Can you please send me proper link…..

  • Etienne

    Hi Naimesh,

    I am a debutant in ABAP OO. I am trying to understand the concept of Widening casting with lots of example found in SDN and your example as well, but still I can’t come to any conclusion on this.
    Well from your example, before doing the widening , we have to do narrow casting..

    Zevolving ABAP Syntax Highlighter

     DATA: lo_animal TYPE REF TO lcl_animal,   " Super Class
            lo_lion          TYPE REF TO lcl_lion,        " Sub class
            lo_tmp_lion   TYPE REF TO lcl_lion.       " Sub class
      CREATE OBJECT lo_tmp_lion.
      lo_animal = lo_tmp_lion.                  " Narrow Casting
    lo_lion ?= lo_animal.     " Widening 

    Here my question is why do we have to assign the instance of lo_animal to lo_lion which is having the same instance of lo_lion( lo_lion & lo_tmp_lion are reference var of same class ). and i also found an another example from one of the SDN thread for widening which i really can’t undersatnd.

    Zevolving ABAP Syntax Highlighter

    DATA lo_tabledescr TYPE REF TO cl_abap_tabledescr.
    DATA lo_structdescr TYPE REF TO cl_abap_structdescr.
    DATA lv_linetypename  TYPE string.
        lo_tabledescr   ?= cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
        lo_structdescr  ?= lo_tabledescr->get_table_line_type( ).
      CATCH cx_sy_move_cast_error.
        MESSAGE 'Error in downcast!' TYPE 'A'.
    lv_linetypename = lo_structdescr->get_relative_name( ).
    WRITE: /, lv_linetypename.
    * Alternative way using dynamic calling
    DATA lo_typedescr_tab TYPE REF TO cl_abap_typedescr.
    DATA lo_typedescr_str TYPE REF TO cl_abap_typedescr.
    lo_typedescr_tab   = cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
        CALL METHOD lo_typedescr_tab->('GET_TABLE_LINE_TYPE')
            p_descr_ref = lo_typedescr_str.
      CATCH cx_sy_dyn_call_error.
        MESSAGE 'Error in dynamic call!' TYPE 'A'.
    lv_linetypename = lo_typedescr_str->get_relative_name( ).
    WRITE: /, lv_linetypename, '...again'.

    Please give me some hints to break through this.
    Thanks in advace,

  • Naimesh Patel

    Hello Eti,

    Casting would be used whenever you want to use the Generic Type e.g. LCL_ANIMAL, CL_ABAP_TYPEDESCR are used to defined the parameter or variables. But they would contains more Specific Type e.g. Reference of LION, or reference of Structure object.

    This code was to just set up the data which would be used for Widening cast later on. Assume that this piece of code is in the Perform or method, and LO_ANIMAL is the returning parameter. So, when you create the object LO_TMP_LION and assign it to LO_ANIMAL, at runtime it would still contain the object reference of the LCL_LION.

    Zevolving ABAP Syntax Highlighter

      CREATE OBJECT lo_tmp_lion.
      lo_animal = lo_tmp_lion.                  " Narrow Casting

    Similar process happens when you use the method DESCRIBE_BY_NAME of the class CL_ABAP_TYPEDESCR. Returning Parameter P_DESCR_REF is declared with the CL_ABAP_TYPEDESCR. But based on the provided importing parameter P_NAME, the method will generate different type of object references. Since the given name in your example is table type, you are receiving that object reference in the variable defined with the type CL_ABAP_TABLEDESCR. Based on the type of the P_NAME system will generate the object so, it doesn’t need to do the narrow casting as what I did in my old and poor example.

    Let me know, if you are still confused.

    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.