2009 Jul 07 2:53 PM
Hi all,
i have two classes 'super' and 'sub'. 'sub' inherits from 'super'.
I have got a reference of type 'super' which points to an object of type 'sub'.
How can i cast the reference from 'super' to 'sub' without knowing the class 'sub'???
With RTTI i just got the name of class 'sub'....
thx in advance.
Regards,
Markus
2009 Jul 07 8:58 PM
Hello Markus,
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).
However, you can dynamically create an object of such a class by using its absolute type name:
CREATE OBJECT go_sub TYPE ('\FUNCTION-POOL=THE_FUNCTION_POOL\CLASS=THE_LOCAL_CLASS').Afterwards, you can invoke methods dynamically:
CALL METHOD go_sub->('THE_METHOD').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.
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).
Hope this helps,
David
Hi all,
i have two classes 'super' and 'sub'. 'sub' inherits from 'super'.
I have got a reference of type 'super' which points to an object of type 'sub'.
How can i cast the reference from 'super' to 'sub' without knowing the class 'sub'???
With RTTI i just got the name of class 'sub'....
thx in advance.
Regards,
Markus
2009 Jul 07 2:55 PM
2009 Jul 07 2:58 PM
Hello again,
i can't use
data: o_sub type ref to sub_class.
because I don't have acces to type 'sub_class'
Markus
Edited by: Markus Bollmann on Jul 7, 2009 3:58 PM
2009 Jul 07 3:01 PM
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.
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.Regards,
Rich Heilman
2009 Jul 07 3:19 PM
Hello Rich,
thanks for your answer - problem is that 'sub_class' is not defined in dicitionary, it is
a local class in a function pool (no acces). So i can't create a new object of it dynamically.
Is it possible to get a sub_class reference from the object i got access via super_class_ref?
(About RTTI,...)
Regards,
Markus
2009 Jul 07 3:57 PM
You can try to create a Public attribute in your Super Class which can hold the reference for the Sub Class.
E.g.
data: O_SUB_REF TYPE REF TO ZCL_SUPER_CLASS.
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.
Regards,
Naimesh Patel
2009 Jul 07 4:01 PM
thx for your answer - unfortunatetly i am not allowed to modify the super_class because it is part of the bus screen framework
Regards,
Markus
2009 Jul 07 4:41 PM
Hello Markus
Perhaps my approach described in
[Accessing the Inacessible - Local Classes within Global Classes|https://wiki.sdn.sap.com/wiki/display/ABAP/AccessingtheInacessible-LocalClasseswithinGlobalClasses]
might be useful for you.
Regards
Uwe
2009 Jul 07 8:58 PM
Hello Markus,
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).
However, you can dynamically create an object of such a class by using its absolute type name:
CREATE OBJECT go_sub TYPE ('\FUNCTION-POOL=THE_FUNCTION_POOL\CLASS=THE_LOCAL_CLASS').Afterwards, you can invoke methods dynamically:
CALL METHOD go_sub->('THE_METHOD').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.
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).
Hope this helps,
David
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |