Skip to Content

Problem with Inheritance in SAP ABAP

I Inherited a Standard Class to cl_cmpic_mail_wrapper_base to CL_CMPIC_MAIL_WRAPPER_BASE to YCL_CMPIC_MAIL_WRAPPER_BASE and written below code.




DATA : lr_obj TYPE REF TO cl_cmpic_mail_wrapper_base,

lr_sub_obj TYPE REF TO ycl_cmpic_mail_wrapper_base,

lr_cx_move TYPE REF TO cx_sy_move_cast_error,

lv_error_txt TYPE string.

CREATE OBJECT lr_obj.

TRY .

lr_sub_obj ?= lr_obj.

CATCH cx_sy_move_cast_error INTO lr_cx_move.

lv_error_txt = lr_cx_move->get_text( ).

WRITE:/ lv_error_txt.

ENDTRY.


As per the standard inheritance the obj lr_sub_obj should get the reference of lr_obj. But it is giving the error like below.



Source type \CLASS=CL_CMPIC_MAIL_WRAPPER_BASE is not compatible, for the purposes of assignment, with target type \CLASS=YCL_CMPIC_MAIL_WRAPPER_BASE. Can any one explain this.


Thanks.

Ravichand.

sub1.PNG (19.8 kB)
sub2.PNG (17.5 kB)
sub3.PNG (6.6 kB)
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    Posted on Jul 18, 2016 at 03:41 PM

    Hi Ravichand,

    The reason is simply that the super class does not contain any definition of the sub class outside of it's own definition. See the following Venn diagram for instance and imagine that A represents the sub class and B represents the super class. Since A extends B then B is a subset of A, but the reverse is not true. For B there is much of A that is not part of B and therefore you cannot safely cast an instance of B as type A.

    Regards,

    Ryan Crosby


    Venn.gif (2.9 kB)
    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Suhas Saha

      The coding tells, that an instance of the super class was created and then there is a try to reference it to a sub class var. That's not possible like Ryan explained. So I guess, that the questioner wanted to create an instance of the sub class and use it like the super class.

  • Posted on Jul 19, 2016 at 08:05 AM

    As most comments say here, there is not a problem with inheritance in SAP ABAP, but you didn't use it correctly.

    The golden rule says that the static type of a reference variable always must be more general or the same as the dynamic type. See the ABAP Keyword Documentation. Assigning a reference pointing to a super class to a subclass reference variable cannot work, because the latter is too specific.This is neither a down (narrowing) cast nor an up (widening) cast but simply not allowed!

    If you want to expose protected attributes of a super class in a subclass, you have to do that programmatically (although it is kind of highly questionable: if the super class decides that something is visible for subclasses only, maybe it's better to keep it inside):

    CLASS sup DEFINITION.

    PROTECTED SECTION.

    DATA prot TYPE string VALUE `protected`.

    ENDCLASS.

    CLASS sub DEFINITION INHERITING FROM sup.

    PUBLIC SECTION.

    METHODS meth RETURNING VALUE(r) LIKE prot.

    ENDCLASS.

    CLASS sub IMPLEMENTATION.

    METHOD meth.

    r = prot.

    ENDMETHOD.

    ENDCLASS.

    DATA: sup_ref TYPE REF TO sup,

    sub_ref TYPE REF TO sub.

    START-OF-SELECTION.

    CREATE OBJECT sup_ref.

    TRY.

    sub_ref ?= sup_ref. "does not work, of course!

    CATCH cx_sy_move_cast_error.

    MESSAGE 'error' TYPE 'I'.

    ENDTRY.

    CREATE OBJECT sub_ref.

    MESSAGE sub_ref->meth( ) TYPE 'I'.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 19, 2016 at 07:53 AM

    You should use

    CREATE OBJECT lr_obj TYPE ycl_cmpic_mail_wrapper_base.


    You could create a method on your subclass that is a getter of the protected attribute. Then you've got it. If you're doing this in some enhancement, then you'd be better served enhancing the superclass with a getter.

    Add a comment
    10|10000 characters needed characters exceeded

    • With lowest priority:

      The problem with your solution

      CREATE OBJECT lr_obj TYPE ycl_cmpic_mail_wrapper_base.

      is that he cannot use lr_obj in order to access any subclass specific method (only dynamically). So it prevents the exception but in fact he must create a subclass reference in a subclass reference variable 😉

  • Posted on Jul 18, 2016 at 04:23 PM

    Thanks for your reply's.

    I'm aware of what all of you had explained. But Please see the below screen shots where im facing the error.

    In Screen Shot 1 and 2 lr_mail contains an attribute called GT_BOL_LINKS Which is protected attribute that we cannot access outside the class. So that i created a subclass ycl_cmpic_mail_wrapper_base Which inherits cl_cmpic_mail_wrapper_base. So that Protected members can be accessed within a Subclass.


    Mail1.PNG (36.3 kB)
    MAIL3.png (24.2 kB)
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 19, 2016 at 01:38 PM

    Another, more elegant (?) way ...

    CLASS sup DEFINITION.

    PROTECTED SECTION.

    DATA attr TYPE string VALUE `protected`.

    ENDCLASS.

    CLASS sub DEFINITION INHERITING FROM sup.

    PUBLIC SECTION.

    METHODS constructor.

    DATA my_attr LIKE sup=>attr.

    ENDCLASS.

    CLASS sub IMPLEMENTATION.

    METHOD constructor.

    super->constructor( ).

    my_attr = attr.

    ENDMETHOD.

    ENDCLASS.

    DATA sub_ref TYPE REF TO sub.

    START-OF-SELECTION.

    CREATE OBJECT sub_ref.

    MESSAGE sub_ref->my_attr TYPE 'I'.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 18, 2016 at 04:02 PM

    Do narrow cast and then a wide cast.

    There are lot of discussions available on this topic.I do not want to explain it again

    You can understand the reason from below links , these should suffice, read it ....

    ABAP Objects: Widening Cast - ABAP Help Blog

    widening casting | SCN

    Widening Cast | SCN

    Add a comment
    10|10000 characters needed characters exceeded

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.