<?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: regarding casting in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909589#M684595</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lokesh...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Narrowing Cast: is used for Generalization to Specialization.&lt;/P&gt;&lt;P&gt;      Here the Super class reference will access the Instance of a Subclass.&lt;/P&gt;&lt;P&gt;      So it can access only the inherited components.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;      Eg: Calculating the Total fuel for all the Flights (both Cargo and Passenger)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref&lt;/P&gt;&lt;P&gt;    Total_fuel = total_fuel + &amp;lt;b&amp;gt;WA_AIRPLANE-&amp;gt;get_fuel( ). "Calls Redefined methods&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;    ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Widening Cast:  is used for Specialization to Generalization .&lt;/P&gt;&lt;P&gt;      Here the Sub class reference will access the Instance of Subclass Which is narrow casted to a Super class reference. that means Widening cast will always use Narrow cast.&lt;/P&gt;&lt;P&gt;      Eg: Calculating the Total Cargo capacity for all the Cargo Flights. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data: ref_ex type ref to cx_root.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref&lt;/P&gt;&lt;P&gt;      TRY .&lt;/P&gt;&lt;P&gt;            &lt;/P&gt;&lt;P&gt;         REF_CARGO ?= WA_AIRPLANE.&lt;/P&gt;&lt;P&gt;         Tot_cargo  = Tot_cargo  +   REF_CARGO-&amp;gt;get_cargo() . "Calls subclass method&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;            CATCH cx_sy_move_cast_error  into ref_ex.&lt;/P&gt;&lt;P&gt;                &lt;/P&gt;&lt;P&gt;        ENDTRY.&lt;/P&gt;&lt;P&gt;     &lt;/P&gt;&lt;P&gt;    ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Reward if Helpful.&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 24 Oct 2007 07:05:35 GMT</pubDate>
    <dc:creator>varma_narayana</dc:creator>
    <dc:date>2007-10-24T07:05:35Z</dc:date>
    <item>
      <title>regarding casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909588#M684594</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi sdns,&lt;/P&gt;&lt;P&gt;i am new to OOABAP.I am being trained on this concepts.I am facing a lot of difficulty in understanding Narrowing Cast &amp;amp; Widening Cast.I have some doubts in that.&lt;/P&gt;&lt;P&gt;i have a doubt in widening cast.I Know that we are increasing the scope with the same object in widening cast.&lt;/P&gt;&lt;P&gt;if we have two classes airplane,cargo_airplane&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: airplane TYPE REF TO lcl_airplane,&lt;/P&gt;&lt;P&gt;cargo_airplane TYPE REF TO lcl_cargo_airplane,&lt;/P&gt;&lt;P&gt;cargo_airplane2 TYPE REF TO lcl_cargo_airplane.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;CREATE OBJECT cargo_airplane.&lt;/P&gt;&lt;P&gt;airplane = cargo_airplane.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cargo_airplane2 ?= airplane.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if we have a method in airplane.we have a redefined it in cargo_airplane.&lt;/P&gt;&lt;P&gt;After this widening cast.&lt;/P&gt;&lt;P&gt;if we acccess this method from cargo_airplane2.&lt;/P&gt;&lt;P&gt;which method will it access redefined method or the method in super class.&lt;/P&gt;&lt;P&gt;please do explain me this example clearly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;with regards,&lt;/P&gt;&lt;P&gt;lokesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2007 06:26:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909588#M684594</guid>
      <dc:creator>lokesh_kamana</dc:creator>
      <dc:date>2007-10-24T06:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: regarding casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909589#M684595</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lokesh...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Narrowing Cast: is used for Generalization to Specialization.&lt;/P&gt;&lt;P&gt;      Here the Super class reference will access the Instance of a Subclass.&lt;/P&gt;&lt;P&gt;      So it can access only the inherited components.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;      Eg: Calculating the Total fuel for all the Flights (both Cargo and Passenger)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref&lt;/P&gt;&lt;P&gt;    Total_fuel = total_fuel + &amp;lt;b&amp;gt;WA_AIRPLANE-&amp;gt;get_fuel( ). "Calls Redefined methods&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;    ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Widening Cast:  is used for Specialization to Generalization .&lt;/P&gt;&lt;P&gt;      Here the Sub class reference will access the Instance of Subclass Which is narrow casted to a Super class reference. that means Widening cast will always use Narrow cast.&lt;/P&gt;&lt;P&gt;      Eg: Calculating the Total Cargo capacity for all the Cargo Flights. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data: ref_ex type ref to cx_root.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref&lt;/P&gt;&lt;P&gt;      TRY .&lt;/P&gt;&lt;P&gt;            &lt;/P&gt;&lt;P&gt;         REF_CARGO ?= WA_AIRPLANE.&lt;/P&gt;&lt;P&gt;         Tot_cargo  = Tot_cargo  +   REF_CARGO-&amp;gt;get_cargo() . "Calls subclass method&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;            CATCH cx_sy_move_cast_error  into ref_ex.&lt;/P&gt;&lt;P&gt;                &lt;/P&gt;&lt;P&gt;        ENDTRY.&lt;/P&gt;&lt;P&gt;     &lt;/P&gt;&lt;P&gt;    ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Reward if Helpful.&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2007 07:05:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909589#M684595</guid>
      <dc:creator>varma_narayana</dc:creator>
      <dc:date>2007-10-24T07:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: regarding casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909590#M684596</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lokesh,&lt;/P&gt;&lt;P&gt;In your example it will access the redefined method.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;see following code snippet.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class airplane.
method check_fuel_level.
endclass.

class cargo_airplane inherited from airplane.
method check_fuel_level redefined.
method get_cargo_weight.
endclass.

class passenger_airplane inherited from airplane.
method check_fuel_level redefined.
method get_passenger_count().
endclass.

DATA: airplane TYPE REF TO airplane,
cargo_airplane  TYPE REF TO cargo_airplane,
cargo_airplane2 TYPE REF TO cargo_airplane,
passenger_airplane TYPE REF TO passanger_airplane.

create object cargo_airplane.
create object passanger_airplane.

* Narrowing Cast
airplane = cargo_airplane.

airplane-&amp;gt;check_fuel_level() . 
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.

airplane-&amp;gt;get_cargo_weight(). 
" is not allowed as the scope of class airplane does not include the method get_cargo_weight.

* Widening Cast
cargo_airplane2 ?= airplane.

airplane-&amp;gt;check_fuel_level(). 
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.
airplane-&amp;gt;get_cargo_weight().
"Will call the method of class cargo_airplane&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hence always the object will call the redefined method irrespective of the reference variable type.&lt;/P&gt;&lt;P&gt;If there is no redefinition available in subclass, it will go one level up in inheritance tree to find the next redefinition.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Geogy&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;PS: Rewards points if the answer is useful.&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2007 09:57:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909590#M684596</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-10-24T09:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: regarding casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909591#M684597</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thwe issue is closed my doubt is solved&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Oct 2007 06:00:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/regarding-casting/m-p/2909591#M684597</guid>
      <dc:creator>lokesh_kamana</dc:creator>
      <dc:date>2007-10-26T06:00:06Z</dc:date>
    </item>
  </channel>
</rss>

