01-03-2018 6:04 AM
I would like to create a table type that holds the objects of different classes and the would be point to a class.
01-03-2018 1:46 PM
Hi Anusha,
If I am getting your question correctly, you want to create a Table Type in data dictionary which could store a reference or object pointing to any class.
Is that correct? If so, you should first create a Structure (Line Type) with fields having Typing Method 'Type ref to' and Component Type as your class name, like depicted in example below.
You can then use this Line Type for the creation of your Table Type using SE11.
Hope this solves your problem.
Thanks & Regards,
Sapeksh
01-03-2018 6:52 AM
If they all implement the same interface, you can use a table of references to that interface. If they're all subclasses of the same super class, then you can use a table of references to that superclass.
Otherwise, you can use a table of references to OBJECT. However, this indicates that you've not designed your class model properly. What is the purpose of your table - what will you use if for and why do you have different objects in it?
01-03-2018 9:13 AM
my question is how to create a table type which has exactly one field that has different classes(objects). by using that i want to call a method in that class??
Thank you.
01-03-2018 9:23 AM
Yes. I understand your question and I've answered it. Which bit of my answer are you having difficulty with?
Calling class methods is well documented - maybe you need to read up on that? Also the concept of "polymorphism".
01-03-2018 9:52 AM
sorry i didn't understand . how to insert different classes into the table type.
01-03-2018 10:06 AM
(Re)read your OO-Abap training materials on down casts and up casts...
01-03-2018 10:11 AM
01-03-2018 12:26 PM
01-03-2018 6:27 PM
This question is still listed as "unanswered". Please see this blog and "do the needful".
01-03-2018 10:01 AM
"and the would be point to a class."
You point to objects not to classes. The pointers are references in reference variables. The golden rule of polymorphism says which reference types can point to which objects.
You have to create a table column with TYPE REF TO otype where otype is a static type that is more general than the classes of your objects. Matthew answered it fully.
The fundamental example of program DEMO_ABAP_OBJECTS contains such an internal table named status_tab.
If you don't understand it, please learn some ABAP (Objects) first.
01-03-2018 1:46 PM
Hi Anusha,
If I am getting your question correctly, you want to create a Table Type in data dictionary which could store a reference or object pointing to any class.
Is that correct? If so, you should first create a Structure (Line Type) with fields having Typing Method 'Type ref to' and Component Type as your class name, like depicted in example below.
You can then use this Line Type for the creation of your Table Type using SE11.
Hope this solves your problem.
Thanks & Regards,
Sapeksh
01-05-2018 7:46 AM
It seems that either you need to learn Object-Oriented Programming, or read the ABAP documentation to learn the syntax in ABAP. Below a few examples of the syntax to help you (this code is dummy / non-sense, just here to show the syntax of constructs).
DATA table_of_any_objects TYPE TABLE OF REF TO OBJECT.
DATA lo_ixml TYPE REF TO if_ixml.
DATA object TYPE REF TO OBJECT.
DATA lo_streamfactory TYPE REF TO IF_IXML_STREAM_FACTORY.
lo_ixml = cl_ixml=>create( ).
APPEND lo_ixml INTO table_of_any_objects.
READ TABLE table_of_any_objects INDEX 1 INTO object.
CALL METHOD object->('CREATE_STREAM_FACTORY') RECEIVING rval = lo_streamfactory.
"--- OR use downcasting ---"
lo_ixml ?= object.
lo_streamfactory = lo_ixml->create_stream_factory( ).
APPEND lo_streamfactory INTO table_of_any_objects.