10-18-2011 10:41 AM
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
10-18-2011 11:44 AM
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
10-18-2011 11:40 AM
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
10-18-2011 11:44 AM
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
10-18-2011 12:24 PM
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').
10-18-2011 12:31 PM
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').
10-18-2011 12:35 PM
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.
10-18-2011 12:46 PM
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
10-18-2011 1:23 PM
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!
10-18-2011 12:37 PM
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
10-18-2011 12:40 PM
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
10-18-2011 1:53 PM
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/]
10-19-2011 5:33 AM
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
10-19-2011 6:08 AM
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
10-19-2011 6:11 AM
Thanks Suhas/Naimesh for the answer (Alternative 3). PLease help me out for my next question regarding Alternative 2