<?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: cast in oops in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645294#M877847</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;NARROWING CAST&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One of the significant principles of inheritance is that an instance from a subclass can be used in every&lt;/P&gt;&lt;P&gt;context in which an instance from the superclass appears. This is possible because the subclass has&lt;/P&gt;&lt;P&gt;inherited all components from the superclass and therefore has the same interface as the superclass. The&lt;/P&gt;&lt;P&gt;user can therefore address the subclass instance in the same way as the superclass instance.&lt;/P&gt;&lt;P&gt;Variables of the type &amp;#147;reference to superclass&amp;#148; can also refer to subclass instances at runtime.&lt;/P&gt;&lt;P&gt;The assignment of a subclass instance to a reference variable of the type &amp;#147;reference to superclass&amp;#148; is&lt;/P&gt;&lt;P&gt;described as a narrowing cast, because you are switching from a view with more detail to a view with less&lt;/P&gt;&lt;P&gt;detail.&lt;/P&gt;&lt;P&gt;The description &amp;#147;up-cast&amp;#148; is also used.&lt;/P&gt;&lt;P&gt;What is a narrowing cast used for? A user who is not interested in the finer points of cargo or passenger&lt;/P&gt;&lt;P&gt;planes (but only, for example, in the tank gauge) does not need to know about them. This user only needs&lt;/P&gt;&lt;P&gt;to work with (references to) the lcl_airplane class. However, in order to allow the user to work with cargo or&lt;/P&gt;&lt;P&gt;passenger planes, you would normally need a narrowing cast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;example&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;


DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
level TYPE ty_level.
CREATE OBJECT cargo_airplane.
* Subclass instance understands the same messages
* as superclass instance
CALL METHOD cargo_airplane-&amp;gt;get_fuel_level RECEIVING re_level = level.
* Narrowing Cast
airplane = cargo_airplane.
* Using the subclass instance in the superclass context
CALL METHOD airplane-&amp;gt;get_fuel_level RECEIVING re_level = level.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After the narrowing cast you can use the airplane reference to access the components of the cargo plane&lt;/P&gt;&lt;P&gt;instance that were inherited from lcl_airplane, obviously in some cases with the limitations entailed by their&lt;/P&gt;&lt;P&gt;visibility. You can no longer access the cargo-plane-specific part of the instance (cargo in the above&lt;/P&gt;&lt;P&gt;example) using the airplane reference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A reference variable always has two types: static and dynamic:&lt;/P&gt;&lt;P&gt;- The static type of a reference variable is determined by variable definition using TYPE REF TO. It cannot&lt;/P&gt;&lt;P&gt;and does not change. It establishes which attributes and methods can be addressed&lt;/P&gt;&lt;P&gt;- The dynamic type of a reference variable is the type of the instance currently being referred to, it is&lt;/P&gt;&lt;P&gt;therefore determined by assignment and can change during the program run. It establishes what coding is&lt;/P&gt;&lt;P&gt;to be executed for redefined methods.&lt;/P&gt;&lt;P&gt;In the example, the static type of the airplane variable is always &amp;#145;REF TO lcl_airplane&amp;#146;, but its dynamic type&lt;/P&gt;&lt;P&gt;after the cast is &amp;#145;REF TO lcl_cargo_airplane&amp;#146;.&lt;/P&gt;&lt;P&gt;The reference ME can be used to determine the dynamic type in the Debugger.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;imp&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The static type of a reference variable&lt;/P&gt;&lt;P&gt;Is determined using TYPE REF TO&lt;/P&gt;&lt;P&gt;Remains constant throughout the program run&lt;/P&gt;&lt;P&gt;Establishes which attributes and methods can be&lt;/P&gt;&lt;P&gt;addressed&lt;/P&gt;&lt;P&gt;The dynamic type of a reference variable&lt;/P&gt;&lt;P&gt;Is determined by assignment&lt;/P&gt;&lt;P&gt;Can change during the program run&lt;/P&gt;&lt;P&gt;Establishes what coding is to be executed for redefined&lt;/P&gt;&lt;P&gt;methods&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;WIDENING CAST&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The type of case described above is known as a widening cast because it changes the view to one with&lt;/P&gt;&lt;P&gt;more details. The instance assigned (a cargo plane in the above example) must correspond to the object&lt;/P&gt;&lt;P&gt;reference (cargo_airplane in the above example), that is, the instance must have the details implied by the&lt;/P&gt;&lt;P&gt;reference.&lt;/P&gt;&lt;P&gt;This is also known as a &amp;#147;down cast&amp;#148;.&lt;/P&gt;&lt;P&gt;The widening cast in this case does not cause an error because the reference airplane actually points to an&lt;/P&gt;&lt;P&gt;instance in the subclass lcl_cargo_airplane. The dynamic type is therefore &amp;#145;REF TO lcl_cargo_airplane&amp;#146;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EXAMPLE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
cargo_airplane2 TYPE REF TO lcl_cargo_airplane.

CREATE OBJECT cargo_airplane.
airplane = cargo_airplane.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;imp about widening cast&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cannot be checked statically&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If unsuccessful, ends with a catchable runtime&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;


CATCH SYSTEM-EXCEPTION MOVE_CAST_ERROR = 4.
cargo_airplane ?= airplane.
ENDCATCH.
IF SY-SUBRC EQ 4.
...
ENDIF.


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be&lt;/P&gt;&lt;P&gt;checked statically, only at runtime. The Cast Operator &amp;#147;?=&amp;#148; (or the equivalent &amp;#147;MOVE ... ?TO &amp;#133;&amp;#148;) must be&lt;/P&gt;&lt;P&gt;used to make this visible.&lt;/P&gt;&lt;P&gt;With this kind of cast, a check is carried out at runtime to ensure that the current content of the source&lt;/P&gt;&lt;P&gt;variable corresponds to the type requirements of the target variables. In this case therefore, it checks that&lt;/P&gt;&lt;P&gt;the dynamic type of the source reference airplane is compatible with the static type of the target reference&lt;/P&gt;&lt;P&gt;cargo_airplane. If it is, the assignment is carried out. Otherwise the catchable runtime error&lt;/P&gt;&lt;P&gt;&amp;#147;MOVE_CAST_ERROR&amp;#148; occurs, and the original value of the target variable remains the same.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;cheers&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sharad&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: sharad narayan on Apr 16, 2008 1:33 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 16 Apr 2008 11:32:22 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2008-04-16T11:32:22Z</dc:date>
    <item>
      <title>cast in oops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645291#M877844</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;what is cast? what is narrowing cast?what is widening  cast?what is ME? plz explai me with good examples.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2008 10:30:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645291#M877844</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-16T10:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: cast in oops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645292#M877845</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;&lt;EM&gt;Narrowing&amp;#148; cast&lt;/EM&gt; means that the assignment changes from a more specialized view (with visibility to more components) to a more generalized view (with visibility to fewer components).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;#147;Narrowing cast&amp;#148; is also referred to as &amp;#147;up cast&amp;#148; . &amp;#147;Up cast&amp;#148; means that the static type of the target variable can only change to higher nodes from the static type of the source variable in the inheritance tree, but not vice versa.&lt;/P&gt;&lt;P&gt;Reference variable of a class assigned to reference variable of class : object&lt;/P&gt;&lt;P&gt;class c1 definition.&lt;/P&gt;&lt;P&gt;endclass.&lt;/P&gt;&lt;P&gt;class c1 implementation.&lt;/P&gt;&lt;P&gt;endclass.&lt;/P&gt;&lt;P&gt;class c2 definition inheriting from c1.&lt;/P&gt;&lt;P&gt;endclass.&lt;/P&gt;&lt;P&gt;class c2 implementation.&lt;/P&gt;&lt;P&gt;endclass.&lt;/P&gt;&lt;P&gt;start-of-selection.&lt;/P&gt;&lt;P&gt;data : oref1 type ref to c1,&lt;/P&gt;&lt;P&gt;oref2 tyep ref to c2.&lt;/P&gt;&lt;P&gt;oref1 = oref2.[/code]&lt;/P&gt;&lt;P&gt;Widening Cast&lt;/P&gt;&lt;P&gt;DATA: o_ref1 TYPE REF TO object, o_ref2 TYPE REF TO class.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;o_ref2 ?= o_ref1.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;CATH SYSTEM-EXCEPTIONS move_cast_error = 4.&lt;/P&gt;&lt;P&gt;o_ref2 ?= o_ref1.&lt;/P&gt;&lt;P&gt;ENDCATCH.[/code]In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator &amp;#147;?=&amp;#147; and catch the potential runtime error move_cast_error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For &lt;EM&gt;widening cast&lt;/EM&gt;, you are partially correc that you do a sub ?= super.&lt;/P&gt;&lt;P&gt;But the pre-condition is that the super should be pointing to a object of sub.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;which means you have done a naroow cast from sub to super earlier (Narrow cast). And now you want to widen the cast back to the sub object. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So eg could be..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sub1 type ref to sub_cls,&lt;/P&gt;&lt;P&gt;sub2 type ref to sub_cls,&lt;/P&gt;&lt;P&gt;super type ref to super_cls. "this is just example. u cant use super as the object name.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;now you do super = sub1. "narrow cast&lt;/P&gt;&lt;P&gt;sub2 = super. "widen the cast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I said, wide casting is possible, only when it has an object of the sub-object but the object is typed to a super class.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope you have now understo the reason why you are getting the error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Raj.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2008 11:09:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645292#M877845</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-16T11:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: cast in oops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645293#M877846</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi check this..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.sap.com/saphelp_nw04s/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/frameset.htm" target="test_blank"&gt;http://help.sap.com/saphelp_nw04s/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/frameset.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="313275"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="528086"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards,&lt;/P&gt;&lt;P&gt;venkat.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2008 11:19:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645293#M877846</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-16T11:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: cast in oops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645294#M877847</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;NARROWING CAST&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One of the significant principles of inheritance is that an instance from a subclass can be used in every&lt;/P&gt;&lt;P&gt;context in which an instance from the superclass appears. This is possible because the subclass has&lt;/P&gt;&lt;P&gt;inherited all components from the superclass and therefore has the same interface as the superclass. The&lt;/P&gt;&lt;P&gt;user can therefore address the subclass instance in the same way as the superclass instance.&lt;/P&gt;&lt;P&gt;Variables of the type &amp;#147;reference to superclass&amp;#148; can also refer to subclass instances at runtime.&lt;/P&gt;&lt;P&gt;The assignment of a subclass instance to a reference variable of the type &amp;#147;reference to superclass&amp;#148; is&lt;/P&gt;&lt;P&gt;described as a narrowing cast, because you are switching from a view with more detail to a view with less&lt;/P&gt;&lt;P&gt;detail.&lt;/P&gt;&lt;P&gt;The description &amp;#147;up-cast&amp;#148; is also used.&lt;/P&gt;&lt;P&gt;What is a narrowing cast used for? A user who is not interested in the finer points of cargo or passenger&lt;/P&gt;&lt;P&gt;planes (but only, for example, in the tank gauge) does not need to know about them. This user only needs&lt;/P&gt;&lt;P&gt;to work with (references to) the lcl_airplane class. However, in order to allow the user to work with cargo or&lt;/P&gt;&lt;P&gt;passenger planes, you would normally need a narrowing cast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;example&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;


DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
level TYPE ty_level.
CREATE OBJECT cargo_airplane.
* Subclass instance understands the same messages
* as superclass instance
CALL METHOD cargo_airplane-&amp;gt;get_fuel_level RECEIVING re_level = level.
* Narrowing Cast
airplane = cargo_airplane.
* Using the subclass instance in the superclass context
CALL METHOD airplane-&amp;gt;get_fuel_level RECEIVING re_level = level.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After the narrowing cast you can use the airplane reference to access the components of the cargo plane&lt;/P&gt;&lt;P&gt;instance that were inherited from lcl_airplane, obviously in some cases with the limitations entailed by their&lt;/P&gt;&lt;P&gt;visibility. You can no longer access the cargo-plane-specific part of the instance (cargo in the above&lt;/P&gt;&lt;P&gt;example) using the airplane reference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A reference variable always has two types: static and dynamic:&lt;/P&gt;&lt;P&gt;- The static type of a reference variable is determined by variable definition using TYPE REF TO. It cannot&lt;/P&gt;&lt;P&gt;and does not change. It establishes which attributes and methods can be addressed&lt;/P&gt;&lt;P&gt;- The dynamic type of a reference variable is the type of the instance currently being referred to, it is&lt;/P&gt;&lt;P&gt;therefore determined by assignment and can change during the program run. It establishes what coding is&lt;/P&gt;&lt;P&gt;to be executed for redefined methods.&lt;/P&gt;&lt;P&gt;In the example, the static type of the airplane variable is always &amp;#145;REF TO lcl_airplane&amp;#146;, but its dynamic type&lt;/P&gt;&lt;P&gt;after the cast is &amp;#145;REF TO lcl_cargo_airplane&amp;#146;.&lt;/P&gt;&lt;P&gt;The reference ME can be used to determine the dynamic type in the Debugger.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;imp&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The static type of a reference variable&lt;/P&gt;&lt;P&gt;Is determined using TYPE REF TO&lt;/P&gt;&lt;P&gt;Remains constant throughout the program run&lt;/P&gt;&lt;P&gt;Establishes which attributes and methods can be&lt;/P&gt;&lt;P&gt;addressed&lt;/P&gt;&lt;P&gt;The dynamic type of a reference variable&lt;/P&gt;&lt;P&gt;Is determined by assignment&lt;/P&gt;&lt;P&gt;Can change during the program run&lt;/P&gt;&lt;P&gt;Establishes what coding is to be executed for redefined&lt;/P&gt;&lt;P&gt;methods&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;WIDENING CAST&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The type of case described above is known as a widening cast because it changes the view to one with&lt;/P&gt;&lt;P&gt;more details. The instance assigned (a cargo plane in the above example) must correspond to the object&lt;/P&gt;&lt;P&gt;reference (cargo_airplane in the above example), that is, the instance must have the details implied by the&lt;/P&gt;&lt;P&gt;reference.&lt;/P&gt;&lt;P&gt;This is also known as a &amp;#147;down cast&amp;#148;.&lt;/P&gt;&lt;P&gt;The widening cast in this case does not cause an error because the reference airplane actually points to an&lt;/P&gt;&lt;P&gt;instance in the subclass lcl_cargo_airplane. The dynamic type is therefore &amp;#145;REF TO lcl_cargo_airplane&amp;#146;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EXAMPLE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
cargo_airplane2 TYPE REF TO lcl_cargo_airplane.

CREATE OBJECT cargo_airplane.
airplane = cargo_airplane.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;imp about widening cast&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cannot be checked statically&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If unsuccessful, ends with a catchable runtime&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;


CATCH SYSTEM-EXCEPTION MOVE_CAST_ERROR = 4.
cargo_airplane ?= airplane.
ENDCATCH.
IF SY-SUBRC EQ 4.
...
ENDIF.


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be&lt;/P&gt;&lt;P&gt;checked statically, only at runtime. The Cast Operator &amp;#147;?=&amp;#148; (or the equivalent &amp;#147;MOVE ... ?TO &amp;#133;&amp;#148;) must be&lt;/P&gt;&lt;P&gt;used to make this visible.&lt;/P&gt;&lt;P&gt;With this kind of cast, a check is carried out at runtime to ensure that the current content of the source&lt;/P&gt;&lt;P&gt;variable corresponds to the type requirements of the target variables. In this case therefore, it checks that&lt;/P&gt;&lt;P&gt;the dynamic type of the source reference airplane is compatible with the static type of the target reference&lt;/P&gt;&lt;P&gt;cargo_airplane. If it is, the assignment is carried out. Otherwise the catchable runtime error&lt;/P&gt;&lt;P&gt;&amp;#147;MOVE_CAST_ERROR&amp;#148; occurs, and the original value of the target variable remains the same.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;cheers&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sharad&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: sharad narayan on Apr 16, 2008 1:33 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2008 11:32:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/cast-in-oops/m-p/3645294#M877847</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-16T11:32:22Z</dc:date>
    </item>
  </channel>
</rss>

