Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

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

arumallaanusha
Explorer

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

1 ACCEPTED SOLUTION

former_member208149
Participant
0 Kudos

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

11 REPLIES 11

matt
Active Contributor

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?

0 Kudos

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.

matt
Active Contributor
0 Kudos

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 Kudos

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

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

0 Kudos

ok .

Thanks for replies..

matt
Active Contributor

INSERT myobjectinstance INTO TABLE mytableofinstances

Jelena
Active Contributor
0 Kudos

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

horst_keller
Product and Topic Expert
Product and Topic Expert

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

former_member208149
Participant
0 Kudos

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

Sandra_Rossi
Active Contributor

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.