<?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: casting in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976339#M702565</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Fighter,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Go to ABAPDOCU tcode and see example programs in abap objects section, you will find separate programs for upcasting and downcasting .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Up-Cast (Widening Cast)&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;Variables of the type &amp;lt;b&amp;gt;reference to superclass&amp;lt;/b&amp;gt; can also refer to subclass instances at runtime.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you assign a subclass reference to a superclass reference, this ensures that&lt;/P&gt;&lt;P&gt;all components that can be accessed syntactically after the cast assignment are&lt;/P&gt;&lt;P&gt;actually available in the instance. The subclass always contains at least the same&lt;/P&gt;&lt;P&gt;components as the superclass. After all, the name and the signature of redefined&lt;/P&gt;&lt;P&gt;methods are identical.&lt;/P&gt;&lt;P&gt;The user can therefore address the subclass instance in the same way as the&lt;/P&gt;&lt;P&gt;superclass instance. However, he/she is restricted to using only the inherited&lt;/P&gt;&lt;P&gt;components.&lt;/P&gt;&lt;P&gt;In this example, after the assignment, the methods GET_MAKE, GET_COUNT,&lt;/P&gt;&lt;P&gt;DISPLAY_ATTRIBUTES, SET_ATTRIBUTES and ESTIMATE_FUEL of the&lt;/P&gt;&lt;P&gt;instance LCL_TRUCK can only be accessed using the reference R_VEHICLE.&lt;/P&gt;&lt;P&gt;If there are any restrictions regarding visibility, they are left unchanged. It is not&lt;/P&gt;&lt;P&gt;possible to access the specific components from the class LCL_TRUCK of the&lt;/P&gt;&lt;P&gt;instance (GET_CARGO in the above example) using the reference R_VEHICLE.&lt;/P&gt;&lt;P&gt;The view is thus usually narrowed (or at least unchanged). That is why we&lt;/P&gt;&lt;P&gt;describe this type of assignment of reference variables as up-cast. There is a&lt;/P&gt;&lt;P&gt;switch from a view of several components to a view of a few components. As&lt;/P&gt;&lt;P&gt;the target variable can accept more dynamic types in comparison to the source&lt;/P&gt;&lt;P&gt;variable, this assignment is also called Widening Cast&lt;/P&gt;&lt;P&gt; Static and Dynamic Types of References&lt;/P&gt;&lt;P&gt;A reference variable always has two types at runtime: static and dynamic.&lt;/P&gt;&lt;P&gt;In the example, LCL_VEHICLE is the static type of the variable R_VEHICLE.&lt;/P&gt;&lt;P&gt;Depending on the cast assignment, the dynamic type is either LCL_BUS or&lt;/P&gt;&lt;P&gt;LCL_TRUCK. In the ABAP Debugger, the dynamic type is specified in the form&lt;/P&gt;&lt;P&gt;of the following object display.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Down-cast (Narrowing Cast)&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;Variables of the type &amp;amp;#147;reference to superclass&amp;amp;#148; can also refer to subclass instances&lt;/P&gt;&lt;P&gt;at runtime. You may now want to copy such a reference (back) to a suitable&lt;/P&gt;&lt;P&gt;variable of the type &amp;amp;#147;reference to subclass&amp;amp;#148;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to assign a superclass reference to a subclass reference, you must&lt;/P&gt;&lt;P&gt;use the down-cast assignment operator MOVE ... ?TO ... or its short form&lt;/P&gt;&lt;P&gt;?=. Otherwise, you would get a message stating that it is not certain that all&lt;/P&gt;&lt;P&gt;components that can be accessed syntactically after the cast assignment are&lt;/P&gt;&lt;P&gt;actually available in the instance. As a rule, the subclass class contains more&lt;/P&gt;&lt;P&gt;components than the superclass.&lt;/P&gt;&lt;P&gt;After assigning this type of reference (back) to a subclass reference to the&lt;/P&gt;&lt;P&gt;implementing class, clients are no longer limited to inherited components: In the&lt;/P&gt;&lt;P&gt;example given here, all components of the LCL_TRUCK instance can be accessed&lt;/P&gt;&lt;P&gt;(again) after the assignment using the reference R_TRUCK2.&lt;/P&gt;&lt;P&gt;The view is thus usually widened (or at least unchanged). That is why we describe&lt;/P&gt;&lt;P&gt;this type of assignment of reference variables as down-cast. There is a switch&lt;/P&gt;&lt;P&gt;from a view of a few components to a view of more components. As the target&lt;/P&gt;&lt;P&gt;variable can accept less dynamic types after the assignment, this assignment is&lt;/P&gt;&lt;P&gt;also called Narrowing Cast&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keep fighting and win the race.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reward points if useful,&lt;/P&gt;&lt;P&gt;Aleem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 05 Nov 2007 03:23:48 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2007-11-05T03:23:48Z</dc:date>
    <item>
      <title>casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976337#M702563</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all&lt;/P&gt;&lt;P&gt;Please give me details about Casting , Upcasting, Down casting.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 04 Nov 2007 13:35:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976337#M702563</guid>
      <dc:creator>former_member329386</dc:creator>
      <dc:date>2007-11-04T13:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976338#M702564</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;The following sample report &amp;lt;b&amp;gt;ZUS_SDN_CL_PO_HEADER_HNDL&amp;lt;/b&amp;gt; shows you how to use casting (into different interface reference types) is used to access the specific methods of a purchase order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;*&amp;amp;---------------------------------------------------------------------*
*&amp;amp; Report  ZUS_SDN_CL_PO_HEADER_HNDL
*&amp;amp;
*&amp;amp;---------------------------------------------------------------------*
*&amp;amp;
*&amp;amp;
*&amp;amp;---------------------------------------------------------------------*

REPORT  zus_sdn_cl_po_header_hndl.


TABLES:  ekko.


DATA:
  go_po_header      TYPE REF TO cl_po_header_handle_mm,
  go_po_doc         TYPE REF TO if_purchasing_document,
  go_po_partner     TYPE REF TO if_partners_mm.


DATA:
  gt_partner        TYPE mmpur_partner_all_tab,
  gt_partner_ext    TYPE mmpur_partner_tab.

PARAMETERS:
  p_ebeln           TYPE ekko-ebeln  DEFAULT '4500016345'.

START-OF-SELECTION.

" Create purchase order instance (header)
  CREATE OBJECT go_po_header
    EXPORTING
      im_po_number = p_ebeln
    EXCEPTIONS
      failure = 1
      OTHERS  = 2.
  IF sy-subrc &amp;lt;&amp;gt; 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
  CHECK ( go_po_header IS BOUND ).


  " Casting into document related instance
  go_po_doc ?= go_po_header.

  gt_partner = go_po_doc-&amp;gt;get_partners( ).
  gt_partner = go_po_header-&amp;gt;if_purchasing_document~get_partners( ).
**  gt_partner = go_po_header-&amp;gt;get_partners( ).  " not possible



  " Casting into partner related instance
  go_po_partner ?= go_po_header.

  CALL METHOD go_po_partner-&amp;gt;get_data
    IMPORTING
      ext_partners = gt_partner_ext.
  CALL METHOD go_po_header-&amp;gt;if_partners_mm~get_data
    IMPORTING
      ext_partners = gt_partner_ext.
**  CALL METHOD go_po_header-&amp;gt;get_data
**    IMPORTING
**      ext_partners = gt_partner_ext.  " not possible


END-OF-SELECTION.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;   Uwe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 04 Nov 2007 14:09:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976338#M702564</guid>
      <dc:creator>uwe_schieferstein</dc:creator>
      <dc:date>2007-11-04T14:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: casting</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976339#M702565</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Fighter,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Go to ABAPDOCU tcode and see example programs in abap objects section, you will find separate programs for upcasting and downcasting .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Up-Cast (Widening Cast)&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;Variables of the type &amp;lt;b&amp;gt;reference to superclass&amp;lt;/b&amp;gt; can also refer to subclass instances at runtime.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you assign a subclass reference to a superclass reference, this ensures that&lt;/P&gt;&lt;P&gt;all components that can be accessed syntactically after the cast assignment are&lt;/P&gt;&lt;P&gt;actually available in the instance. The subclass always contains at least the same&lt;/P&gt;&lt;P&gt;components as the superclass. After all, the name and the signature of redefined&lt;/P&gt;&lt;P&gt;methods are identical.&lt;/P&gt;&lt;P&gt;The user can therefore address the subclass instance in the same way as the&lt;/P&gt;&lt;P&gt;superclass instance. However, he/she is restricted to using only the inherited&lt;/P&gt;&lt;P&gt;components.&lt;/P&gt;&lt;P&gt;In this example, after the assignment, the methods GET_MAKE, GET_COUNT,&lt;/P&gt;&lt;P&gt;DISPLAY_ATTRIBUTES, SET_ATTRIBUTES and ESTIMATE_FUEL of the&lt;/P&gt;&lt;P&gt;instance LCL_TRUCK can only be accessed using the reference R_VEHICLE.&lt;/P&gt;&lt;P&gt;If there are any restrictions regarding visibility, they are left unchanged. It is not&lt;/P&gt;&lt;P&gt;possible to access the specific components from the class LCL_TRUCK of the&lt;/P&gt;&lt;P&gt;instance (GET_CARGO in the above example) using the reference R_VEHICLE.&lt;/P&gt;&lt;P&gt;The view is thus usually narrowed (or at least unchanged). That is why we&lt;/P&gt;&lt;P&gt;describe this type of assignment of reference variables as up-cast. There is a&lt;/P&gt;&lt;P&gt;switch from a view of several components to a view of a few components. As&lt;/P&gt;&lt;P&gt;the target variable can accept more dynamic types in comparison to the source&lt;/P&gt;&lt;P&gt;variable, this assignment is also called Widening Cast&lt;/P&gt;&lt;P&gt; Static and Dynamic Types of References&lt;/P&gt;&lt;P&gt;A reference variable always has two types at runtime: static and dynamic.&lt;/P&gt;&lt;P&gt;In the example, LCL_VEHICLE is the static type of the variable R_VEHICLE.&lt;/P&gt;&lt;P&gt;Depending on the cast assignment, the dynamic type is either LCL_BUS or&lt;/P&gt;&lt;P&gt;LCL_TRUCK. In the ABAP Debugger, the dynamic type is specified in the form&lt;/P&gt;&lt;P&gt;of the following object display.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;Down-cast (Narrowing Cast)&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;Variables of the type &amp;amp;#147;reference to superclass&amp;amp;#148; can also refer to subclass instances&lt;/P&gt;&lt;P&gt;at runtime. You may now want to copy such a reference (back) to a suitable&lt;/P&gt;&lt;P&gt;variable of the type &amp;amp;#147;reference to subclass&amp;amp;#148;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to assign a superclass reference to a subclass reference, you must&lt;/P&gt;&lt;P&gt;use the down-cast assignment operator MOVE ... ?TO ... or its short form&lt;/P&gt;&lt;P&gt;?=. Otherwise, you would get a message stating that it is not certain that all&lt;/P&gt;&lt;P&gt;components that can be accessed syntactically after the cast assignment are&lt;/P&gt;&lt;P&gt;actually available in the instance. As a rule, the subclass class contains more&lt;/P&gt;&lt;P&gt;components than the superclass.&lt;/P&gt;&lt;P&gt;After assigning this type of reference (back) to a subclass reference to the&lt;/P&gt;&lt;P&gt;implementing class, clients are no longer limited to inherited components: In the&lt;/P&gt;&lt;P&gt;example given here, all components of the LCL_TRUCK instance can be accessed&lt;/P&gt;&lt;P&gt;(again) after the assignment using the reference R_TRUCK2.&lt;/P&gt;&lt;P&gt;The view is thus usually widened (or at least unchanged). That is why we describe&lt;/P&gt;&lt;P&gt;this type of assignment of reference variables as down-cast. There is a switch&lt;/P&gt;&lt;P&gt;from a view of a few components to a view of more components. As the target&lt;/P&gt;&lt;P&gt;variable can accept less dynamic types after the assignment, this assignment is&lt;/P&gt;&lt;P&gt;also called Narrowing Cast&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keep fighting and win the race.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reward points if useful,&lt;/P&gt;&lt;P&gt;Aleem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Nov 2007 03:23:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting/m-p/2976339#M702565</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-11-05T03:23:48Z</dc:date>
    </item>
  </channel>
</rss>

