<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Diff Between widening Cast &amp; Narrowing Casting in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642307#M877136</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;widening means when u type cast a char to integer it is called widening that means type casting lower to higher one this is also called as upcasting.&lt;/P&gt;&lt;P&gt;narrowing means when u type cast integer to char it is called narrowing that means type casting higher to lower one this is also called  down casting.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;reward if helpful.&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;amith&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 19 Apr 2008 11:43:45 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2008-04-19T11:43:45Z</dc:date>
    <item>
      <title>Diff Between widening Cast &amp; Narrowing Casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642306#M877135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What are differences between &lt;STRONG&gt;widening Cast &amp;amp; Narrowing Casting&lt;/STRONG&gt; ?&lt;/P&gt;&lt;P&gt;Give some exaples also.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Apr 2008 10:38:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642306#M877135</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-19T10:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: Diff Between widening Cast &amp; Narrowing Casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642307#M877136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;widening means when u type cast a char to integer it is called widening that means type casting lower to higher one this is also called as upcasting.&lt;/P&gt;&lt;P&gt;narrowing means when u type cast integer to char it is called narrowing that means type casting higher to lower one this is also called  down casting.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;reward if helpful.&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;amith&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Apr 2008 11:43:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642307#M877136</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-19T11:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Diff Between widening Cast &amp; Narrowing Casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642308#M877137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey fren,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Narrowing Cast is also known as Upcasting...&lt;/P&gt;&lt;P&gt;Widening Cast is known as Downcasting...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let us take an example...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We define a class lcl_vehicle and other class lcl_truck inherited from lcl_vehicle.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_vehicle DEFINITION.

      PUBLIC SECTION.
         METHODS constructor
                   IMPORTING im_make_v  TYPE string.
                   
         METHODS estimate_fuel
                   IMPORTING im_distance   TYPE s_distance
                   RETURNING value(re_fuel) TYPE s_capacity
      
         METHODS get_make........
                                        ........
          
         METHODS get_count........
                                        ........
          METHODS display_attributes........
                                        ........
      PROTECTED SECTION.
          DATA r_tank TYPE REF TO lcl_tank.
      
      PRIVATE SECTION.
          DATA make TYPE string.

ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.
          
          METHOD constructor.
               make = im_make_v.
          ENDMETHOD.
          METHOD get_make.
             - - - 
             - - - 
          ENDMETHOD.
          METHOD get_count.
             - - -
             - - -
          ENDMETHOD.
          METHOD display_attributes.
             - - -
             - - -
          ENDMETHOD.

ENDCLASS.


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now we define a sub class lcl_truck to super class lcl_vehicle.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.

       PUBLIC SECTION.
            METHODS constructor IMPORTING im_make_t TYPE string
                                                         im_cargo_t TYPE s_plan_car
            METHODS estimate_fuel REDEFINITION.
            METHODS get_cargo RETURNING value( re_cargo ) TYPE s_plan_car.
       PRIVATE SECTION.
          DATA max_cargo TYPE s_plan_car.
ENDCLASS.

CLASS lcl_truck IMPLEMENTATION.
      METHOD constructor.
           CALL METHOD super-&amp;gt;constructor( im_make_v  = im_make_t ).
            max_cargo = im_cargo_t.
      ENDMETHOD.

      METHOD estimete_fuel.
          DATA total_weight TYPE w_weight.
          total_weight = max_cargo + weight.
          re_fuel = total_weight * im_distance * correction_factor.
       ENDMETHOD.                    
ENDCLASS.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now we again define one more subclass lcl_bus to superclass lcl_vehicle.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle.

PUBLIC SECTION.
            METHODS constructor IMPORTING im_make_b TYPE string
                                                         im_load_b TYPE s_plan_car

       PRIVATE SECTION.
          DATA max_load TYPE s_plan_car.
ENDCLASS.  

CLASS lcl_bus IMPLEMENTATION.
      METHOD constructor.
           CALL METHOD super-&amp;gt;constructor( im_make_v  = im_make_b ).
            max_cargo = im_load_b.
      ENDMETHOD.

      METHOD estimete_fuel.
          DATA total_weight TYPE w_weight.
          total_weight = max_passengers * average_weight + weight.
          re_fuel = total_weight * im_distance * correction_factor.
       ENDMETHOD.                    
ENDCLASS. 
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Variables of the type &lt;STRONG&gt;&lt;EM&gt;'reference to superclass'&lt;/EM&gt;&lt;/STRONG&gt; can also refer to subclass instances at runtime.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is called as Narrowing Casting...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lets see how it works...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

* Creating references to the classes.........

DATA:
    r_vehicle TYPE REF TO lcl_vehicle,
    r_truck    TYPE REF TO lcl_truck,
    r_bus      TYPE REF TO lcl_bus.

*Creating an object of class lcl_truck.........

CREATE OBJECT r_truck.

*Narrowing Cast ................

r_vehicle = r_truck.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now reference --&amp;gt; superclass.... r_vehicle will point to only the inherited compenents of lcl_truck.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;If you assign a subclass reference to a superclass reference, this ensures that all components that can be accessed syntactically after the cast assignment are actually available in the instance. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;The subclass always contains at least the same components as the superclass. After all, the name and the signature of redefined methods are identical. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;The user can, therefore, address the subclass instance in the same way as the superclass instance. However, he is &lt;STRONG&gt;restricted&lt;/STRONG&gt; to using only the inherited components. In this example, after the assignment, the methods GET_MAKE, GET_COUNT, DISPLAY_ATTRIBUTES and ESTIMATE_FUEL of the instance LCL_TRUCK can only be accessed using the reference R_VEHICLE. If there are any restrictions regarding visibility, they are left unchanged. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;It is &lt;STRONG&gt;not possible&lt;/STRONG&gt; to access the specific components from the class LCL_TRUCK of the instance (GET_CARGO in the above example) using the reference R_VEHICLE. The view is thus usually narrowed (or at least unchanged). That is why we describe this type of assignment of reference variables as narrowing cast. There is a switch from a view of several components to a view of a few components. The term &lt;STRONG&gt;&lt;EM&gt;upcast&lt;/EM&gt;&lt;/STRONG&gt; is also common.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;A typical use for narrowing cast assignments is to prepare for generic access. A user who is not at all interested in the finer points of the instances of the subclasses but who simply wants to address the shared components could use a superclass reference for this access.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the example shown here, assume that a travel agency (LCL_RENTAL) needs to manage all imaginable kinds of vehicles in one list. This leads to the question of what types should be assigned to the internal table for the references to airplane instances. You should also assume that the car rental company needs to be able to calculate only the required amount of fuel for all its vehicles.  Correspondingly, the ESTIMATE_FUEL method is defined in the superclass LCL_VEHICLE and is redefined in all subclasses.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is about Narrowing Cast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Inspire if this was needful,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Warm Regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Abhi...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Apr 2008 15:13:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642308#M877137</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-19T15:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: Diff Between widening Cast &amp; Narrowing Casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642309#M877138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey Fren,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now lets discuss about Widening Cast....in this post..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Variables of the type &lt;STRONG&gt;&lt;EM&gt;reference to superclass&lt;/EM&gt;&lt;/STRONG&gt; can also refer to subclass instances at runtime.You may want to copy such a reference back to a suitable variable of the type reference to subclass.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let us have the same example code to be continued in here....&lt;/P&gt;&lt;P&gt;We have lcl_vehicle as superclass and lcl_truck and lcl_bus as subclass.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As we have created the object of r_truck in the previous post, here we will create one more reference &lt;STRONG&gt;r_truck2&lt;/STRONG&gt; to class lcl_truck.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA:
    r_truck2 LIKE lcl_truck.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So First we will use Narrowing Cast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
*Narrowing Cast
      r_vehicle = r_truck.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;While 'Widening Cast' we have the special symbol....&lt;EM&gt;&lt;STRONG&gt;?=&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
*Widening Cast.........
       r_truck2 ?= r_vehicle.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;If you want to assign a superclass reference to a subclass reference, you must use the widening cast assignment operator&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;MOVE ... ?TO ... or its short form ?=. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Otherwise, you will get a message stating that it is not certain that all components that can be accessed syntactically after the cast assignment are actually available in the instance. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;As a rule, the subclass class contains more components than the superclass. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;After assigning this type of reference back to a subclass reference to the implementing class, clients are no longer limited to inherited components. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;In the example given here, all components of the LCL_TRUCK instance can be accessed again after the assignment using the reference R_TRUCK2. The view is thus usually widened, or at least unchanged. That is why we describe this type of assignment of reference variables as widening cast; there is a switch from a view of a few components to a view of more components. &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The term downcast is also common.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;A typical use for widening cast assignments is when specific components of instances need to be addressed and their references are kept in variables that are typed on the superclass. A user who is interested in the finer points of the instances of a subclass cannot use the superclass reference for this access because it only allows access to the shared components.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;In this example, a car rental company (LCL_RENTAL) wants to determine the maximum capacity of its trucks, but it stores all types of vehicle references in an internal table that is typed as LCL_VEHICLE. Therefore, what happens if there is no truck reference in the superclass reference R_VEHICLE at runtime but the widening cast assignment operator tries to copy the reference to the now invalid reference R_TRUCK?&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is about widening cast....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this is needful to you.....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Inspire by rewarding,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Warm Regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Abhi....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Apr 2008 16:16:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/diff-between-widening-cast-narrowing-casting/m-p/3642309#M877138</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-19T16:16:08Z</dc:date>
    </item>
  </channel>
</rss>

