ABAP Objects: Narrowing Cast

By | September 13, 2008 | ABAP Objects, OO Concepts | 22,949 | 15
Important principal of the Inheritence is that an instance of the Subclass can be used in all the contexts where the Superclass can be used. This is possible because the Subclass contains all the attributes of the Superclass, because Subclass has been inhertied from the Super.

When we assign the instance of the Subclass back to the instance of the Superclass, than it is called the “Narrow Casting”, because we are switching from a “More Specific view of an object” to “less specific view”.

For example: we have a class a global class for ANIMAL. Now, we have inherited more specific class as LION from the ANIMAL. Since LION will have all the attributes from the ANIMAL, we can always refer back to ANIMAL with the reference of the LION.

When this Narrow casting is used?

An application which is interested in the details of the LION, but don’t want to know about them. An application only needs to work with the ANIMAL class. In order to allow the application to access the attributes of the LION we have to do a narrow cast and assign it back to the ANIMAL, which is visible from an application.

UML diagram for the example

Lets check out the UML of the Demo.

In words: LCL_ANIMAL class has a method HUNGRY( ). LCL_LION is inherited from the LCL_ANIMAL. Program is using the LCL_LION to access the instance.

Code Lines

Lets checkout the code lines.

*&---------------------------------------------------------------------*
*& This code snippet implements the local calss ANIMAL and LION
*&---------------------------------------------------------------------*
REPORT  ztest_narrow_casting.
*
*----------------------------------------------------------------------*
* Animal Super Class defintion
*----------------------------------------------------------------------*
CLASS lcl_animal DEFINITION.
  PUBLIC SECTION.
    METHODShungry.
ENDCLASS.                    "lcl_animal DEFINITION
*
*----------------------------------------------------------------------*
* Lion subclass defintion
*----------------------------------------------------------------------*
CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
    METHODShungry REDEFINITION,
             fasting.
ENDCLASS.                    "lcl_lion DEFINITION
*
*
*----------------------------------------------------------------------*
* Animal Implementation
*----------------------------------------------------------------------*
CLASS lcl_animal IMPLEMENTATION.
  METHOD hungry.
    WRITE'An animal is hungry'.
  ENDMETHOD.                    "hungry
ENDCLASS.                    "lcl_animal IMPLEMENTATION
*
*----------------------------------------------------------------------*
* Lion subclass implementation
*----------------------------------------------------------------------*
CLASS lcl_lion IMPLEMENTATION.
  METHOD hungry.
    WRITE'A Lion (King of Jungle) is hungry.',
             'Run as fast as you can..!'.
  ENDMETHOD.                    "hungry
  METHOD fasting.
    WRITE'Stop running. Lion is on Fasting today.'.
  ENDMETHOD.
ENDCLASS.                    "lcl_lion IMPLEMENTATION

*----------------------------------------------------------------------*
* This code shows how to use the Narrow casting
*----------------------------------------------------------------------*
START-OF-SELECTION.
  DATAlo_animal TYPE REF TO lcl_animal,
        lo_lion   TYPE REF TO lcl_lion.
*
* ANIMAL object without NARROW casting
  WRITE'Animal - without NARROW casting'.
  CREATE OBJECT lo_animal.
  CALL METHOD lo_animal->hungry).
  CLEAR lo_animal.
*
* ANIMAL object with NARROW CASTING
  SKIP 2.
  WRITE'Animal -  NARROW casting from LION'.
  CREATE OBJECT lo_lion.
  lo_animal lo_lion.
  CALL METHOD lo_animal->hungry).
*
* call the method FASTING must be a dynamic because FASTING is not
*   in the Animal
  CALL METHOD lo_animal->('FASTING').

Output from this code snippet:

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

Naimesh Patel{245 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 245 articles.

Load comments

15 Comments

  • Anonymous

    Thanky u for the explanation!
    Excellent!!

  • Hi Naimesh,

    I saw that the terms “up cast”, “down cast”, “narrowing cast” and “widening cast” often get mixed up. According to the official ABAP documentation published by SAP e.g., a narrowing cast and a down cast are the same. They would also consider your example as an up cast, but as a widening cast, not a narrowing one.

    See http://help.sap.com/abapdocu/en/ABENDOWN_CAST_GLOSRY.htm
    and http://help.sap.com/abapdocu/en/ABENUP_CAST_GLOSRY.htm

  • Hello Chirstian,


    I saw that the terms “up cast”, “down cast”, “narrowing cast” and “widening cast” often get mixed up. According to the official ABAP documentation published by SAP e.g., a narrowing cast and a down cast are the same. They would also consider your example as an up cast, but as a widening cast, not a narrowing one.

    See http://help.sap.com/abapdocu/en/ABENDOWN_CAST_GLOSRY.htm
    and http://help.sap.com/abapdocu/en/ABENUP_CAST_GLOSRY.htm

    It seems something wrong is going on. If you see the ABAP Glossary in the Online help (F1 Help) you will see the below listed defintion for Narrowing Cast:

    Narrowing Cast
    Also called Up Cast. Assignment between reference variables, where the static type of the target variables is more general than or the same as the static type of the source variables. See also Widening Cast.

    And also According to SAP Course on ABAP Object (BC404):

    Page 6-16
    Variables of the type “reference to superclass” can also refer to subclass instances at runtime.
    The assignment of a subclass instance to a reference variable of the type “reference to superclass” is
    described as a narrowing cast, because you are switching from a view with more detail to a view with less detail.
    The description “up-cast” is also used.

    Regards,
    Naimesh Patel

  • You’re right Naimesh. Looks like SAP changed its mind on the definition of the casting terms:

    The F1 help of our AS ABAP 6.20 says you need ?= only for widening casts, while the F1 help of our AS ABAP 7.0 says you need ?= only for narrowing casts.

    The BC404 course already existed for release 4.6C, so it probably still contains SAP’s superseded casting definition, too.

    Horst Keller uses the 7.0 definition of the terms in his books “The Official ABAP Reference” (2005) and “ABAP Objects” (2007):

    http://www.galileodesign.de/buchscanner/abschnitt/gp/buchscanId-147701704?GalileoSession=67378616A34Yy7hK2UI

    http://www.galileodesign.de/buchscanner/abschnitt/gp/buchscanId-147701707?GalileoSession=67378616A34Yy7hK2UI

    (If the above links don’t work, go to http://www.galileodesign.de/buchscanner and search for “narrowing cast”. You will find results both in German and in English books.)

  • Hello Christian, 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.
    http://help.sap.com/abapdocu/en/ABENROOT_CLASS_GLOSRY.htm

    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.

    I guess, I have to mention this point in my blog and always consider NARROWING CAST or WIDENING CAST terms, don’t go with the UP or DOWN casting :-)

    Regards,
    Naimesh Patel

  • Anonymous

    Hello all,

    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@

    Kind regards

    Horst Keller
    ABAP Documentation, SAP AG

  • Thanks Mr. Horst Keller for the link. It ends the speculations!

    Regards,
    Naimesh Patel

  • Anonymous

    Great Example!

  • Anonymous

    Very well explained.

  • John

    thanks for the great explanation.

    i will refer Widening Cast right now

  • Anonymous

    excellent

  • Anonymous

    why should we do narrow casting or wide casting ?

    if you instantiate the subclass. You can access the methods of both sub class and super class. Then, why we need to assign the subclass instance to the super class. Please any body help me regarding this.

    Thanks,
    pal

  • Hello,

    You have asked
    "why should we do narrow casting or wide casting ?

    if you instantiate the subclass. You can access the methods of both sub class and super class. Then, why we need to assign the subclass instance to the super class. Please any body help me regarding this."

    To design good and robust solutions, we should use the abstract class/interface in the client(caller) rather than the concrete class. So, at time you use the Abstract class (super class) to define the object reference and at run time, you should decide which object to use. This object at runtime would be the instance of the subclass. In other words, caller doesn't know the exact class until runtime.

    Regards,
    Naimesh Patel

  • Anonymous

    Simple and best…great!!!!
    Thanks

  • Anonymous

    Hi,

    In the above example the methods for class lcl_lion can be called using the reference lo_lion directly, I mean using reference of class lcl_lion we can use all components of that class where as after narrowimg cast we ca only use inherited components, then what is the benefit of casting and using reference of super class?

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