Skip to Content
author's profile photo Former Member
Former Member

ABAP Polymorphism Problem

Hello All!

I am running into a problem of type compatibility when I declare a parameter of a subroutine as an abstract superclass and try to pass through a subclass. According to the documentation I have read, this should work (it also works in many other OOP languages). Furthermore, I attempted to create a field symbol of the superclass and assign the subclass to it, then pass it through to the subroutine, but the type incompatability error occurred there as well. This leads me to believe it is a problem with my declaration of the classes, but they seem alright. I am going to post example code in the following post.

Please examine and let me know if I am doing anything weird/wrong.

Thank you,

Steven Coolbaugh

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Aug 26, 2009 at 04:04 PM

    You need to do the casting to the super's reference and use this object in the parameter of your Subroutine.

    Like:

    *----------------------------------------------------------------------*
    *       CLASS lcl_super DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_super DEFINITION ABSTRACT.
    
      PROTECTED SECTION.
        DATA: f_num1 TYPE i.
    
    ENDCLASS.                    "lcl_super DEFINITION
    
    *----------------------------------------------------------------------*
    *       CLASS lcl_sub DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
    
      PUBLIC SECTION.
        METHODS:
          set_f_num1
            IMPORTING
              if_num TYPE i,
          get_f_num1
            RETURNING
              value(rf_num) TYPE i.
    
    ENDCLASS.                    "lcl_sub DEFINITION
    
    START-OF-SELECTION.
      DATA: o_super TYPE REF TO lcl_super,
            o_sub   TYPE REF TO lcl_sub.
    
      CREATE OBJECT o_sub.
    
      o_sub->set_f_num1( 10 ).
      o_super = o_sub.                     " Down Casting
      PERFORM get_f_num USING o_super.
    
    *&---------------------------------------------------------------------*
    *&      Form  get_f_num
    *&---------------------------------------------------------------------*
    FORM get_f_num USING io_super TYPE REF TO lcl_super.
    
      DATA: lo_sub TYPE REF TO lcl_sub,
            lf_num TYPE i.
    
      lo_sub ?= io_super.      " Up Casting
    
      lf_num = lo_sub->get_f_num1( ).
      WRITE: lf_num.
    
    ENDFORM.                    "get_f_num
    
    *----------------------------------------------------------------------*
    *       CLASS lcl_sub IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_sub IMPLEMENTATION.
    
      METHOD set_f_num1.
        me->f_num1 = if_num.
      ENDMETHOD.                                                "set_F_num1
    
      METHOD get_f_num1.
        rf_num = me->f_num1.
      ENDMETHOD.                                                "get_f_num1
    
    ENDCLASS.                    "lcl_sub IMPLEMENTATION
    

    Regards,

    Naimesh Patel

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Naimesh Patel

      Ah, once again my previous experience is causing problems. In C++, declarations of pointers to an object have a different declaration from object variable declarations. I keep forgetting that ABAP uses reference variables exclusively, therefore the object is not instanced (although had I thought about it, CREATE OBJECT would not need to be used if it worked otherwise). Thank you for the correction.

      On a different note, I did not have to up cast the passed super object to the sub object; which is very good, since I am passing many different subclasses through to this subroutine.

      Once again, thank you for your help.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.