Application Development and Automation 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: 
Read only

Confuse using ABAP Object...

Former Member
0 Likes
601

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?

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
563

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

4 REPLIES 4
Read only

former_member480923
Active Contributor
0 Likes
563

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.

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
563

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

Read only

uwe_schieferstein
Active Contributor
0 Likes
564

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

Read only

0 Likes
563

Thanks Uwe Schieferstein

I clear my problem.

Thanks for the help.

Really appreciated