<?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 Casting of object reference in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828546#M1316183</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;&lt;/P&gt;&lt;P&gt;i have two classes 'super' and 'sub'. 'sub' inherits from 'super'. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have got a reference of type 'super' which points to an object of type 'sub'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can i cast the reference from 'super' to 'sub' without knowing the class 'sub'???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With RTTI i just got the name of class 'sub'....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thx in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Markus&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Jul 2009 13:53:23 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2009-07-07T13:53:23Z</dc:date>
    <item>
      <title>Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828546#M1316183</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;&lt;/P&gt;&lt;P&gt;i have two classes 'super' and 'sub'. 'sub' inherits from 'super'. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have got a reference of type 'super' which points to an object of type 'sub'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can i cast the reference from 'super' to 'sub' without knowing the class 'sub'???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With RTTI i just got the name of class 'sub'....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thx in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Markus&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 13:53:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828546#M1316183</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-07-07T13:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828547#M1316184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use the ?= operator.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data: o_sub type ref to sub_class.
data: o_super type ref to super_class.

o_sub ?= o_super.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 13:55:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828547#M1316184</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2009-07-07T13:55:16Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828548#M1316185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello again,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i can't use &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;data: o_sub type ref to sub_class.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;because I don't have acces to type 'sub_class'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Markus&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Markus Bollmann on Jul 7, 2009 3:58 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 13:58:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828548#M1316185</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-07-07T13:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828549#M1316186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well,  you can still cast by using the ?=.  Thought that was your question.  If you want to create a reference to a class dynamically at runtime, then you must do something like this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data: o_super type ref to zcl_super_class
data: o_sub type ref to object.
data: lv_sub_class type char20 value 'ZCL_SUB_CLASS'.

create object o_sub type (lv_sub_class).

o_sub ?= o_super.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&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;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 14:01:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828549#M1316186</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2009-07-07T14:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828550#M1316187</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Rich,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks for your answer - problem is that 'sub_class' is not defined in dicitionary, it is&lt;/P&gt;&lt;P&gt;a local class in a function pool (no acces). So i can't create a new object of it dynamically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is it possible to get a sub_class reference from the object i got access via super_class_ref? &lt;/P&gt;&lt;P&gt;(About RTTI,...)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Markus&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 14:19:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828550#M1316187</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-07-07T14:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828551#M1316188</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can try to create a Public attribute in your Super Class which can hold the reference for the Sub Class.&lt;/P&gt;&lt;P&gt;E.g.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
data:  O_SUB_REF TYPE REF TO ZCL_SUPER_CLASS.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Later on, if you have an access of the Super Class object, you get an access of the Sub Class. Keep in mind that, you will able to access the inherited attributes without any dynamics but to access all other Sub Class specific attributes, you need to call them dynamically, may be by SETTER (SET_attribute) and GETTER (GET_attribute) methods.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naimesh Patel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 14:57:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828551#M1316188</guid>
      <dc:creator>naimesh_patel</dc:creator>
      <dc:date>2009-07-07T14:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828552#M1316189</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thx for your answer - unfortunatetly i am not allowed to modify the super_class because it is part of the bus screen framework&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Markus&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 15:01:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828552#M1316189</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-07-07T15:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828553#M1316190</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Markus&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Perhaps my approach described in&lt;/P&gt;&lt;P&gt;[Accessing the Inacessible - Local Classes within Global Classes|https://wiki.sdn.sap.com/wiki/display/ABAP/Accessing&lt;EM&gt;the&lt;/EM&gt;Inacessible&lt;EM&gt;-&lt;/EM&gt;Local&lt;EM&gt;Classes&lt;/EM&gt;within&lt;EM&gt;Global&lt;/EM&gt;Classes]&lt;/P&gt;&lt;P&gt;might be useful for you.&lt;/P&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>Tue, 07 Jul 2009 15:41:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828553#M1316190</guid>
      <dc:creator>uwe_schieferstein</dc:creator>
      <dc:date>2009-07-07T15:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: Casting of object reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828554#M1316191</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Markus,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think you cannot cast the second object reference, since you cannot define an object reference of the actual type of the subclass as target (because the type is not visible statically when it is defined in another program).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, you can dynamically create an object of such a class by using its &lt;EM&gt;absolute type name&lt;/EM&gt;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CREATE OBJECT go_sub TYPE ('\FUNCTION-POOL=THE_FUNCTION_POOL\CLASS=THE_LOCAL_CLASS').&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Afterwards, you can invoke methods dynamically:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CALL METHOD go_sub-&amp;gt;('THE_METHOD').&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, the syntax checker cannot verify these method calls during design time. To ensure that a specified method is callable for a given object during runtime, you can use RTTI before doing the call.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As already discussed in this forum, it is not good practice to use local classes from other programs. They can have dependencies to their context (e.g. a global variable of the program where they are defined).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 19:58:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/casting-of-object-reference/m-p/5828554#M1316191</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-07-07T19:58:46Z</dc:date>
    </item>
  </channel>
</rss>

