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: 

Reason for Short Dump???

Former Member
0 Kudos

Why does this program terminate even when i_obj has a RUNTIME TYPE of TYPE REF TO Class C1.

Note: I_OBJ is defined as TYPE REF TO interface, however can hold a object of class C1. At run time I_OBJ points to object of ckass C1, However CALL METHOD I_OBJ->('CALL_DUMMY') terminates with a dump.

Please explain

Report xyz.

interface intf.
  methods : dummy.
endinterface.

class c1 definition.
  public section.
    interfaces intf.
    methods : call_dummy.
endclass.

class c1 implementation.
  method intf~dummy.
    write : 'HI, Dummy Here'.
  endmethod.

  method call_dummy.
    intf~dummy( ).
  endmethod.
endclass.

START-OF-SELECTION.
  DATA : i_obj TYPE REF TO intf.

  create object i_obj type c1.
  call method i_obj->('CALL_DUMMY').

<Added code tags>

Edited by: palwaigupta on Oct 18, 2011 11:55 AM

Moderator Message: Please use code tags to format your code.

Edited by: Suhas Saha on Oct 18, 2011 3:28 PM

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Dear Palwaigupta

Here's your code corrected.

This works.

The correction is on the last line of the code.


REPORT xyz.
*----------------------------------------------------------------------*
*       INTERFACE intf
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE intf.
  METHODS : dummy.
ENDINTERFACE.                    "intf

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
  PUBLIC SECTION.
    INTERFACES intf.
    METHODS : call_dummy.
ENDCLASS.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
  METHOD intf~dummy.
    WRITE : 'HI, Dummy Here'.
  ENDMETHOD.                    "intf~dummy

  METHOD call_dummy.
    intf~dummy( ).
  ENDMETHOD.                    "call_dummy
ENDCLASS.                    "c1 IMPLEMENTATION

START-OF-SELECTION.
  DATA : i_obj TYPE REF TO intf.

  CREATE OBJECT i_obj TYPE c1.
  CALL METHOD i_obj->('DUMMY').

Kind Regards

/Ricardo Quintas

13 REPLIES 13

Former Member
0 Kudos

Hi,

It dumps as the method "CALL_DUMMY' is not available in the interface 'inter'. i_obj holds an object of class C1 at run-time, but i_obj is of type interface 'intf'. Interface should be used to call interface specific methods and not class specific methods. Class specific method 'CALL_DUMMY' can be called only when the object variable(i_obj) is declared as the type of class as shown below.


  call method i_obj->('DUMMY').

  DATA i_obj_cl TYPE REF TO c1. " Class reference variable
  i_obj_cl ?= i_obj.
  call method i_obj_cl->('CALL_DUMMY').

Regards,

Varun

Former Member
0 Kudos

Dear Palwaigupta

Here's your code corrected.

This works.

The correction is on the last line of the code.


REPORT xyz.
*----------------------------------------------------------------------*
*       INTERFACE intf
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE intf.
  METHODS : dummy.
ENDINTERFACE.                    "intf

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 DEFINITION.
  PUBLIC SECTION.
    INTERFACES intf.
    METHODS : call_dummy.
ENDCLASS.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
  METHOD intf~dummy.
    WRITE : 'HI, Dummy Here'.
  ENDMETHOD.                    "intf~dummy

  METHOD call_dummy.
    intf~dummy( ).
  ENDMETHOD.                    "call_dummy
ENDCLASS.                    "c1 IMPLEMENTATION

START-OF-SELECTION.
  DATA : i_obj TYPE REF TO intf.

  CREATE OBJECT i_obj TYPE c1.
  CALL METHOD i_obj->('DUMMY').

Kind Regards

/Ricardo Quintas

0 Kudos

Hello Richardo,

I dont have to use

I_OBJ->('DUMMY'), since i can anyways use I_OBJ->DUMMY directly.

Hello Varun,

I am aware that by doing casting obj_c1 ?= i_obj I can call the CALL_DUMMY sucessfuly.

what I meant was If i done using a inheritance instead of using an interface, I could have called the method CALL_DUMMY using an object of TYPE REF TO super class(Parent Class).

I wanted to know why its not using Interface Reference Variable.

for ex:

 

class c0 definition.
  public section.
  methods : dummy.
endclass.

class c0 implementation.
  method dummy.
    write : 'HI, Dummy Here'.
  endmethod.
endclass.

class c1 definition inheriting from c0.
  public section.
    methods : call_dummy.
endclass.

class c1 implementation.
  method call_dummy.
    dummy( ).
  endmethod.
endclass.

START-OF-SELECTION.
  DATA : i_obj TYPE REF TO c0.

  create object i_obj type c1.
  call method i_obj->('CALL_DUMMY').

0 Kudos

For me this code is not giving any dump


REPORT a.
*---------------------------------------------------------------------*
*       CLASS c0 DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS c0 DEFINITION.
  PUBLIC SECTION.
    METHODS : dummy.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS c1 DEFINITION INHERITING FROM c0.
  PUBLIC SECTION.
    METHODS : call_dummy.
ENDCLASS.


*---------------------------------------------------------------------*
*       CLASS c0 IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS c0 IMPLEMENTATION.
  METHOD dummy.
    WRITE : 'HI, Dummy Here'.
  ENDMETHOD.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
  METHOD call_dummy.
    CALL METHOD dummy( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA : i_obj TYPE REF TO c0.

  CREATE OBJECT i_obj TYPE c1.
  CALL METHOD i_obj->('CALL_DUMMY').

0 Kudos

Dear Keshav,

Thats what i wanted to stress that Using Inheritance DOES NOT give any dump, whereas using interfaces does gives dump?

I am looking for the technical reason for why it happens.

0 Kudos

Hello All,

To be honest i was not aware of this behaviour

But when in dilemma always fall back to good ol' SAP documentation.

The search for this method takes place only in the static type of iref.

Source: http://help.sap.com/abapdocu_702/en/abapcall_method_meth_ident_dyna.htm#!ABAP_ALTERNATIVE_3@3@.

BR,

Suhas

Edited by: Suhas Saha on Oct 18, 2011 5:21 PM

0 Kudos

Hello!

I think that´s because you have to change you object declaration as follow:

START-OF-SELECTION.

DATA : i_obj TYPE REF TO c1.

create object i_obj .

call method i_obj->CALL_DUMMY

Your object must be ref to the class witch implements the interface, and not directly to the interface.

Or you can call the interface method without use the method Call dummy:

START-OF-SELECTION.

DATA : i_obj TYPE REF TO c1.

create object i_obj .

call method i_obj->intf~DUMMY

I hope it helps you!

kesavadas_thekkillath
Active Contributor
0 Kudos

Hi,

As im using a old version od sap..I am unable to check your code.

Its because the method call_dummy doent exist within your interface.

Kesav

0 Kudos

Dear Keshav

I know I havent included the method "CALL_DUMMY" In the interface, which i have done similarly in the class C0, but there it works not in case of interfaces

naimesh_patel
Active Contributor
0 Kudos

You are trying to access a component which doesn't exist in the interface. When you call the method using the interface reference, that method MUST exist in the Interface. You can't call any method which doesn't exist in the interface (intf) but in the concrete class (C1).

If you have variable was declared with reference to Class, you can call any method from the class dynamically.

From Keyword help on Dynamic Method call:

Alternative 3

... iref->(meth_name) ... .

Effect

: This form is possible for all visible interface methods of objects. iref can be any interface reference variable that points to an object that contains the interface method specified in meth_name.

Note

In contrast to access using class reference variables, interface reference variables can only be used to access interface components, and not to access all components, even with dynamic access.

Help can be found at: http://help.sap.com/abapdocu_702/en/abapcall_method_meth_ident_dyna.htm#!ABAP_ALTERNATIVE_3@3@

Regards,

[Naimesh Patel|http://help-abap.zevolving.com/]

0 Kudos

Dear Naimesh/Suhas,

Thanks for the link, I was going thru the link and something struck me.My Alternative 3 question is answered.

Out of curiosity I am asking this.

In Alternative 2 its mentioned that

Alternative 2

... cref->(meth_name) ... .

Effect

This form is possible for all visible methods of objects. cref can be any class reference variable that points to an object that contains the method specified in meth_name. This method is searched for first in the static type, then in the dynamic type of cref

So, If a object OBJ_C1 of type ref to Superclass C1 calls a method say M1 which is redifined in child class C2.

then if OBJ_C1 is pointing to instance of C2, and we use OBJ_C1->('M1'), As per Alternative 2, First the search for M1 is made in the static type of Object OBJ_C1 which is M1 of class C1 and then in the subclass C2. so why then Dynamic call

OBJ_C1->('M1') triggers the redifined method as if we have called it like OBJ_C1->M1 (not Dynamically)

Note: OBJ_C1->M1 does not trigger dynamic search as its a static call.

Whereas OBJ_C2->('M1') triggers SEARCH for M1 in static type and then in dynamic type if not found in static Type

Edited by: palwaigupta on Oct 19, 2011 6:34 AM

0 Kudos

I Think I should be closing this threas since the question is answered (Alternative 3) for reason for the dump...I can raise the next question (Alternative 2) as a new thread altogether.

Thanks Suhas/Naimesh

Former Member
0 Kudos

Thanks Suhas/Naimesh for the answer (Alternative 3). PLease help me out for my next question regarding Alternative 2