Skip to Content
-4

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

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

Add comment
10|10000 characters needed 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.

  • Get RSS Feed

3 Answers

  • Best Answer
    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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 03 at 06: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?

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 05 at 07: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.
    
    Add comment
    10|10000 characters needed characters exceeded