When this Narrow casting is used?
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.
METHODS: hungry.
ENDCLASS. "lcl_animal DEFINITION
*
*----------------------------------------------------------------------*
* Lion subclass defintion
*----------------------------------------------------------------------*
CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.
PUBLIC SECTION.
METHODS: hungry 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.
DATA: lo_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 …
- ABAP Objects: Overriding (Redefinition)
- ABAP Objects: Widening Cast
- CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?
- READ-ONLY attribute vs GETTER methods
- ABAP Objects Concepts – Friends
- Class-based Exceptions I – Basics
- Become a JAVAPER – is overloading possible in ABAP?
- Override (Redefine) Static Method?
- Abstract Class vs Interface – Which to use when?
- When to use Local Class and when not to!
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
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
Great Example!
Very well explained.
thanks for the great explanation.
i will refer Widening Cast right now
excellent
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
Simple and best…great!!!!
Thanks
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?