‎2007 Jan 12 7:59 AM
Hi all..
I declare instance like below..
DATA: l_test TYPE REF TO ycl_test_001,
l_object TYPE REF TO object,
CREATE OBJECT l_test.
CREATE OBJECT l_object TYPE ('YCL_TEST_001').
CALL METHOD l_test->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
is activated but
CALL METHOD l_object->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
is not activated
What is the difference?
‎2007 Jan 12 10:58 AM
Hello Ho
Please have a look at the comment of the sample report.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_OO_CASTING
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zus_sdn_oo_casting.
*---------------------------------------------------------------------*
* CLASS lcl_class DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS ycl_test_001 DEFINITION.
PUBLIC SECTION.
METHODS:
test
IMPORTING
i_num TYPE i
EXPORTING
e_result TYPE i.
ENDCLASS. "ycl_test_001 DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_class IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS ycl_test_001 IMPLEMENTATION.
METHOD test.
ENDMETHOD. "test
ENDCLASS. "ycl_test_001 IMPLEMENTATION
DATA:
l_test TYPE REF TO ycl_test_001,
l_object TYPE REF TO object,
l_cast TYPE REF TO ycl_test_001,
*
l_num1 type i,
l_num2 type i.
START-OF-SELECTION.
CREATE OBJECT l_test.
CREATE OBJECT l_object TYPE ('YCL_TEST_001').
CALL METHOD l_test->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
CREATE OBJECT l_object TYPE ('YCL_TEST_001').
* CALL METHOD l_object->test
* EXPORTING
* i_num = l_num1
* IMPORTING
* e_result = l_num2.
* NOTE: syntax error because the static reference type of
* l_object is OBJECT which does not have a method TEST.
* Casting required !!!
l_cast ?= l_object.
CALL METHOD l_cast->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
* NOTE: static reference type of l_cast is YCL_TEST_001 and,
* therefore, knows method TEST.
END-OF-SELECTION.Regards
Uwe
‎2007 Jan 12 9:39 AM
Hi,
The class gets instantiated only during runtime and so the methods starts getting visible only after the class is initiated. Please refer to the following program for reference
INTERFACE I1.
...
ENDINTERFACE.
CLASS C1 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
...
ENDCLASS.
CLASS C3 DEFINITION.
PUBLIC SECTION.
METHODS CREATE_OBJECT IMPORTING CLASS_NAME TYPE C
EXPORTING POINTER TYPE REF TO OBJECT.
ENDCLASS.
CLASS C3 IMPLEMENTATION.
METHOD CREATE_OBJECT.
CREATE OBJECT POINTER TYPE (CLASS_NAME).
ENDMETHOD.
ENDCLASS.
DATA: R1 TYPE REF TO I1,
R2 TYPE REF TO C2,
R3 TYPE REF TO C3,
R TYPE REF TO OBJECT.
START-OF-SELECTION.
CREATE OBJECT R3.
CALL METHOD: R3->CREATE_OBJECT EXPORTING CLASS_NAME = 'C1'
IMPORTING POINTER = R.
CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 4.
R1 ?= R.
ENDCATCH.
IF SY-SUBRC EQ 0.
WRITE / 'OK'.
ENDIF.
CALL METHOD: R3->CREATE_OBJECT EXPORTING CLASS_NAME = 'C2'
IMPORTING POINTER = R.
CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 4.
R2 ?= R.
ENDCATCH.
IF SY-SUBRC EQ 0.
WRITE / 'OK'.
ENDIF.
Hope that Helps............
Thanks
Anirban M.
‎2007 Jan 12 9:50 AM
HI,
At the compilation time it is clearly eviden that in the first case the the parameter you are passing are the same that the constructor of the class 'YCL_TEST_001'.
Where as OBJECT's constructor does not have this signature so you will get error and it will not be activated.
Regards,
Sesh
‎2007 Jan 12 10:58 AM
Hello Ho
Please have a look at the comment of the sample report.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_OO_CASTING
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zus_sdn_oo_casting.
*---------------------------------------------------------------------*
* CLASS lcl_class DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS ycl_test_001 DEFINITION.
PUBLIC SECTION.
METHODS:
test
IMPORTING
i_num TYPE i
EXPORTING
e_result TYPE i.
ENDCLASS. "ycl_test_001 DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_class IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS ycl_test_001 IMPLEMENTATION.
METHOD test.
ENDMETHOD. "test
ENDCLASS. "ycl_test_001 IMPLEMENTATION
DATA:
l_test TYPE REF TO ycl_test_001,
l_object TYPE REF TO object,
l_cast TYPE REF TO ycl_test_001,
*
l_num1 type i,
l_num2 type i.
START-OF-SELECTION.
CREATE OBJECT l_test.
CREATE OBJECT l_object TYPE ('YCL_TEST_001').
CALL METHOD l_test->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
CREATE OBJECT l_object TYPE ('YCL_TEST_001').
* CALL METHOD l_object->test
* EXPORTING
* i_num = l_num1
* IMPORTING
* e_result = l_num2.
* NOTE: syntax error because the static reference type of
* l_object is OBJECT which does not have a method TEST.
* Casting required !!!
l_cast ?= l_object.
CALL METHOD l_cast->test
EXPORTING
i_num = l_num1
IMPORTING
e_result = l_num2.
* NOTE: static reference type of l_cast is YCL_TEST_001 and,
* therefore, knows method TEST.
END-OF-SELECTION.Regards
Uwe
‎2007 Jan 15 2:28 AM
Thanks Uwe Schieferstein
I clear my problem.
Thanks for the help.
Really appreciated