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: 

call super->super->method( )?

daniel_humberg
Contributor
0 Kudos

I have a class CL_LION that inherits from CL_CAT which inherits from CL_ANIMAL.

In CL_ANIMAL, I have a method shout( ), which is overwritten in each of the children.

Now, I want to call super->super->shout() from CL_LION (which means I want to skip the shout() of CL_CAT and directly call shout() of CL_ANIMAL.

The syntax "super->super->shout() does not work.

Is there another way to do this?

Best regards,

Daniel

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate

I believe you can do this using casting.

try this.

cl_animal ?= cl_lion.

cl_animal->shout( ).

Regards,

Rich Heilman

15 REPLIES 15

RichHeilman
Developer Advocate
Developer Advocate

I believe you can do this using casting.

try this.

cl_animal ?= cl_lion.

cl_animal->shout( ).

Regards,

Rich Heilman

0 Kudos

Hi Rich,

i tried


DATA lr_animal TYPE REF TO cl_animal.
lr_animal ?= me.
lr_animal->shout( ).

This however still calls shout() of CL_LION, since lr_animal is still a reference to CL_LION and not to CL_ANIMAL.

So this leads into an endless loop....

0 Kudos

Yes, I've just tested it, it is still a "Lion". See the example program, this works, but I'm not really sure of your requirement so it may not work out for you. Notice that in each redefintion, I'm just calling the super->shout in each method.




report zrich_0001.


*---------------------------------------------------------------------*
*       CLASS lcl_animal  DEFINTION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal definition.
  public section.
    methods: shout.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_animal IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal implementation.

  method shout.
    write:/ 'This is an animal shouting'.

  endmethod.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat definition <b>inheriting from lcl_animal.</b>
  public section.
    methods shout  redefinition.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat implementation.

  method shout.
<b>    call method super->shout( ).</b>
  endmethod.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion definition <b>inheriting from lcl_cat.</b>
  public section.
    methods shout  redefinition.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion implementation.

  method shout.
<b>    call method super->shout( ).</b>
  endmethod.

endclass.


data: a_lion type ref to lcl_lion.
data: a_animal type ref to lcl_animal.


start-of-selection.

  create object a_lion.

  call method a_lion->shout( ).

Regards,

Rich Heilman

0 Kudos

Hi Rich,

the problem is that I want to skip the call of cl_cat->shout(). In your example, this method is called.

Regards, Daniel

0 Kudos

Ok, so you want to go directly back to the animal class. How about we use an INTERFACE.



report zrich_0001.



*---------------------------------------------------------------------*
*       INTERFACE lif_animal
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
<b>interface lif_animal .
  methods shout.
endinterface.</b>

*---------------------------------------------------------------------*
*       CLASS lcl_animal  DEFINTION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal definition.


  public section.

<b>    interfaces lif_animal.</b>
    methods: shout.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_animal IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal implementation.

  method shout.
    write:/ 'This is an animal shouting'.

  endmethod.

<b>  method lif_animal~shout.
    write:/ 'This is an animal shouting'.
  endmethod.</b>

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat definition inheriting from lcl_animal.
  public section.
    methods shout  redefinition.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat implementation.

  method shout.
    call method super->shout( ).
  endmethod.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion definition inheriting from lcl_cat.
  public section.
    methods shout  redefinition.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion implementation.

  method shout.
    call method super->shout( ).
  endmethod.

endclass.


data: a_lion type ref to lcl_lion.
data: a_animal type ref to lcl_animal.
<b>data: if_animal type ref to lif_animal.</b>


start-of-selection.

  create object a_lion.
<b>  if_animal ?= a_lion.
  call method if_animal->shout( ).</b>

Regards,

Rich Heilman

0 Kudos

I havn't checked this, but I assume that this could still call the interface method shout( ) of CL_LION because also the interface and it's method are inherited from above.

0 Kudos

I have tested it, it is calling the method from the animal class. Just copy and paste that sample program and run it, you will see.

REgards,

Rich heilman

0 Kudos

Hello Rich,

there is a slight mistake in your sample the methods shout() and if_animal~shout() are 2 different methods. It is simply impossible to call the super->super->method( ).

@Daniel: If you really require such a feature try following fallback:

Introduce a protected method _Shout() in the animal class, which gets called from the Shout() method. As long you dont override this method, you can delegate also in subclasses to this method.

But I wonder why this is necessary at all.

Best Regards

Klaus

0 Kudos

Your exactly right, Klaus. They are two different methods. Not sure what I was thinking there.

Regards,

RIch Heilman

0 Kudos

Not sure why I thought that the INTERFACE would be a solution. I makes no sense. Thanks Klaus. Anyway, I guess the only solution is to NOT redefine the method Shout in the children classes, then when you call the method Shout it would be triggered int the animal class.



report zrich_0001.





*---------------------------------------------------------------------*
*       CLASS lcl_animal  DEFINTION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal definition.


  public section.
    methods: shout.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_animal IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_animal implementation.

  method shout.
    write:/ 'This is an animal shouting'.

  endmethod.


endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat definition inheriting from lcl_animal.


endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_cat IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_cat implementation.


endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion definition inheriting from lcl_cat.
  public section.


endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_lion IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_lion implementation.



endclass.


data: a_lion type ref to lcl_lion.
data: a_animal type ref to lcl_animal.



start-of-selection.

create object a_lion.

call METHOD a_lion->shout( ).

Regards

Rich Heilman

0 Kudos

Hi Klaus.

Ok, there is no super->super.

Sure, I can create another method and put everything that should be used by all subclasses in there.

What I tried in the moment it to modify the CL_CAT class (the class "in the middle"). In it's shout method, I did the following:


method shout.
super->shout( ).

DATA lr_cat TYPE REF TO cl_cat.
CREATE OBJECT lr_cat.
IF get_classname( me ) <> get_classname( lr_cat ).
  RETURN.
ENDIF.

PS: the method get_classname was written by my using cl_abap_classdescr=>describe_by_object_ref.

now very elegant, but at least it works

0 Kudos

Hi,

so if you want to use the original shout( ) method in your sub-sub-class why don't you just call it? To get clearer:

class cl_lion implementation.

method shout.

data ref_animal type ref to cl_animal.

create object ref_animal.

ref_animal->shout( ).

endmethod.

endclass.

Regards

Tobias

0 Kudos

Hello Tobias,

that will not work. Just try to debug the shout() method.

Regards

Klaus

0 Kudos

Hi Tobias,

this would call the shout method, but not of the correct object. If the current object has instance attributes, the new object lr_animal would not have these attribut values.

Regards,

D.

0 Kudos

Hi Daniel,

yes you're right. If the Method shout changes or uses instance attributes, the new object would use different ones. I think you're solution is a nice way to keep the behavoir of cl_cat exclusively in cat objects. But maybe you should have the chance to decide weather the super->shout or the cl_cat-shout method is processed. In example if you have another class cl_cheetha and call in your method cl_cheetha->shout super->shout( ). Wouldn't you expect to get the behavoir of the cl_cat class instead of the cl_animal? So maybe you could give the name of the class you want to shout to your shout method.

ref_lion->shout ( exporting class = cl_animal ).

Regards

Tobias