<?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: downcasting question in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583530#M1566607</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;what is the difference between upcasting(narrowing cast) and downcasting( widening cast).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 03 Feb 2011 07:10:28 GMT</pubDate>
    <dc:creator>former_member751136</dc:creator>
    <dc:date>2011-02-03T07:10:28Z</dc:date>
    <item>
      <title>downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583525#M1566602</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;Class zlcl_cargo_plane is a subclass of zlcl_airplane. Is the statement r_cargo ?= r_plane in the below code downcasting? What is the purpose of this downcasting? Is it to restrict the determination of highest cargo value to cargo planes only? In debug mode, statement r_cargo ?= r_plane fails if pointer r_plane doesn't point to a cargo plane, and an exception is caught. But I thought that downcasting in this example would restrict the methods available to class zlcl_cargo_plane to those available in class zlcl_airplane.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;method GET_HIGHEST_CARGO.&lt;/P&gt;&lt;P&gt;  DATA: r_cargo TYPE REF TO zlcl_cargo_plane,&lt;/P&gt;&lt;P&gt;        r_plane TYPE REF TO zlcl_airplane,&lt;/P&gt;&lt;P&gt;        cargo TYPE s_plan_car,&lt;/P&gt;&lt;P&gt;        r_exc TYPE REF TO cx_root.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  LOOP AT airplane_list INTO r_plane.&lt;/P&gt;&lt;P&gt;    TRY.&lt;/P&gt;&lt;P&gt;        r_cargo ?= r_plane.  "downcasting u2013 fails if plane is NOT cargo plane,&lt;/P&gt;&lt;P&gt;                                         "and exception cx_sy_move_cast_error is caught&lt;/P&gt;&lt;P&gt;        cargo = r_cargo-&amp;gt;get_cargo( ).&lt;/P&gt;&lt;P&gt;        IF re_cargo &amp;lt; cargo.&lt;/P&gt;&lt;P&gt;          re_cargo = cargo. &lt;/P&gt;&lt;P&gt;        ENDIF.&lt;/P&gt;&lt;P&gt;      CATCH cx_sy_move_cast_error INTO r_exc.&lt;/P&gt;&lt;P&gt;    ENDTRY.&lt;/P&gt;&lt;P&gt;  ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;endmethod.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Jan 2011 21:41:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583525#M1566602</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-01-11T21:41:20Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583526#M1566603</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello John,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Is the statement r_cargo ?= r_plane in the below code downcasting? What is the purpose of this downcasting? Is it to restrict the determination of highest cargo value to cargo planes only?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Down-cast (Narrowing Cast) is a technique to assign a super class reference to a sub class reference. There is a switch from a view of a few components to a view of more components. One of the purposes of down casting is to allow you to use the specific components of the subclass instances when these instances are kept in references that are typed on the super class. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hence, in your example if the instance referred by r_plane is of type Cargo plane you would not be able to directly use the methods / attributes defined in the class Cargo plane. So in order to use the methods / attributes defined in the class Cargo plane you will have to down-cast it to a reference of type Cargo plane. I am assuming that the get_cargo method is defined in the zlcl_cargo_plane class. Hence you would not be able to use this method directly from r_plane. So in order to call the method you will have to down-cast the generic type r_plane to a more specific type r_cargo. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;In debug mode, statement r_cargo ?= r_plane fails if pointer r_plane doesn't point to a cargo plane, and an exception is caught.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The run time system checks before making such an assignment if the contents in the source variable (r_plane) correspond to the type requirement of the target variable (r_cargo). If the check fails then an exception is raised. Hence, if the r_plane refers to an instance of some other type of plane then you will get an exception. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;But I thought that downcasting in this example would restrict the methods available to class zlcl_cargo_plane to those available in class zlcl_airplane.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is up-cast. So if you would want to restrict the methods available to class zlcl_cargo_plane to those available in the class zlcl_airplane you have to up-cast it; r_plane = r_cargo. Now only the methods / attributes defined by zlcl_airplane would be available from the reference r_plane. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Ramneek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Jan 2011 02:48:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583526#M1566603</guid>
      <dc:creator>Ramneek</dc:creator>
      <dc:date>2011-01-12T02:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583527#M1566604</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN __default_attr="green" __jive_macro_name="color"&gt;&lt;STRONG&gt;Basic rule of reference variable assigment&lt;/STRONG&gt;: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;"The static type should be more generic than or equal to the dynamic type of the reference value". &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN __default_attr="green" __jive_macro_name="color"&gt;&lt;STRONG&gt;What does this mean?&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Static type &lt;STRONG&gt;r_cargo&lt;/STRONG&gt; should be more more generic than the dynamic type &lt;STRONG&gt;r_plane&lt;/STRONG&gt; in your case. But since &lt;STRONG&gt;r_cargo&lt;/STRONG&gt; is the sub-class of &lt;STRONG&gt;r_plane&lt;/STRONG&gt; it is less generic than the latter, hence normal assigment cannot be made.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN __default_attr="green" __jive_macro_name="color"&gt;&lt;STRONG&gt;Why we need down cast?&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;If you try to assign r_plane to r_cargo, the static syntax check will give an error(since basic rule is violated). In this case the casting operator '?=' is used to suppress the static syntax check from throwing the error!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope i'm clear with my explanation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Suhas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Jan 2011 05:30:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583527#M1566604</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2011-01-12T05:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583528#M1566605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ramneek,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much for your thorough answers to my questions. It is very clear to me now.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Jan 2011 13:45:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583528#M1566605</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-01-12T13:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583529#M1566606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Suhas,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your help. I really appreciate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Jan 2011 13:47:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583529#M1566606</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-01-12T13:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583530#M1566607</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;what is the difference between upcasting(narrowing cast) and downcasting( widening cast).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Feb 2011 07:10:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583530#M1566607</guid>
      <dc:creator>former_member751136</dc:creator>
      <dc:date>2011-02-03T07:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: downcasting question</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583531#M1566608</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This message was moderated.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Mar 2011 06:43:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/downcasting-question/m-p/7583531#M1566608</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-03-12T06:43:19Z</dc:date>
    </item>
  </channel>
</rss>

