‎2012 Feb 02 3:41 AM
Hi Experts
I am new to ABAP Objects, but I know Java. So I am trying to learn ABAP comparing with Java but it creating lot of confusions.
in a demo program I found the below code :
DATA tab_root TYPE REF TO cl_wd_uielement_container.
DATA structure_root TYPE REF TO cl_wd_uielement_container.
DATA new_tab TYPE REF TO cl_wd_table.
IF wd_this->structure_changed = abap_true.
structure_root ?= view->get_element( id = 'FIELD_GROUP' ).
tab_root ?= view->get_element( id = 'TABLE_GROUP' ).
In the above code snippet,
structure_root ?= view->get_element( id = 'FIELD_GROUP' ).is a downcasting and view is of type ref to IF_WD_VIEW and class cl_wd_uielement_container implements this interface.
My question is, when we do not know which is the implemenation class for this interface, how can we call a method of this interface (because methods are not implemented in the interface itself but in the implementation class).??
In the above assignment statement, how does system finds out which implementation to point to??
Question may be very basic, but i couldnt find the answer for this.
Please help me
Thanks
Dhananjay Hegde
‎2012 Feb 02 5:52 AM
Hi,
First create the instance(object) of class in you were implement interface method then call the interface method using the object.
for example
Report Zinterface,
interface IF_view.
methods: view.
endinterface.
class c1 definition.
public section.
interfaces: IF_view.
methods:m1,m2. " here some more methods.
endclass.
class c1 implementation.
method IF_view~view.
write:/ 'this is an interface method'.
endmethod.
method m1.
write:/ 'this is M1 method'.
endmethod.
method m2.
write:/ 'this is M2 method'.
endmethod.
endclass.
data: obj type ref to c1.
START-OF-SELECTION.
create object obj.
call method obj->IF_view~view." call the interface method using the insatance of where you were implemeted the interfaceThe above procedure apply for your requirement.. i hope you may get idea about interface method calling.
Best Regards
sreenivas.
‎2012 Feb 02 5:19 AM
I either didn't understand your question or you answered it yourself. You can't call a method of an interface, only of a class that implements said interface.
‎2012 Feb 02 5:24 AM
Yes, but if you see in the above statements,
'view' is declared as this ->
view type ref to if_wd_view(as a formal parameter to the method WDDOMODIFYVIEW in a webdynpro application. you can refer the webdynpro app DEMODYNAMIC in SE80 for this).
Am trying to understand this. Please help me.
‎2012 Feb 02 5:52 AM
Hi,
First create the instance(object) of class in you were implement interface method then call the interface method using the object.
for example
Report Zinterface,
interface IF_view.
methods: view.
endinterface.
class c1 definition.
public section.
interfaces: IF_view.
methods:m1,m2. " here some more methods.
endclass.
class c1 implementation.
method IF_view~view.
write:/ 'this is an interface method'.
endmethod.
method m1.
write:/ 'this is M1 method'.
endmethod.
method m2.
write:/ 'this is M2 method'.
endmethod.
endclass.
data: obj type ref to c1.
START-OF-SELECTION.
create object obj.
call method obj->IF_view~view." call the interface method using the insatance of where you were implemeted the interfaceThe above procedure apply for your requirement.. i hope you may get idea about interface method calling.
Best Regards
sreenivas.
‎2012 Feb 02 6:13 AM
Hi Both
Thank you so much for your reply.
But, somehow I found how it is working in the above case.
In here they are passing the ref variable of a class that implements the interface 'if_wd_view' to the method WDDOMODIFYVIEW.
hence they could call the method directly using the interface ref instead of the class ref.
Anyway, thank you so much for your efforts