Skip to Content
-4

how to create a table type that holds the objects of different clases?

Jan 03 at 06:04 AM

136

avatar image
Former Member

I would like to create a table type that holds the objects of different classes and the would be point to a class.

10 |10000 characters needed characters left characters exceeded

"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.

2
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
SAPEKSH GUPTA Jan 03 at 01:46 PM
-3

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


te.png (17.4 kB)
Share
10 |10000 characters needed characters left characters exceeded
Matthew Billingham
Jan 03 at 06:52 AM
3

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?

Show 7 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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.

0

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".

0
Former Member

sorry i didn't understand . how to insert different classes into the table type.

0

(Re)read your OO-Abap training materials on down casts and up casts...

2

INSERT myobjectinstance INTO TABLE mytableofinstances

1
Former Member

ok .

Thanks for replies..

0

This question is still listed as "unanswered". Please see this blog and "do the needful".

0
Sandra Rossi Jan 05 at 07:46 AM
1

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.
Share
10 |10000 characters needed characters left characters exceeded