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

Type Casting - Clarification

Former Member
0 Likes
1,208

Hi Experts,

I am aware of type casting and how it works but need some clarification

I have sub class reference and super class reference declared

Super class reference = Sub class reference   (CORRECT) - no syntax error and no exception

Sub class reference = Super class reference   (INCORRECT) - gives syntax error

Sub class reference ?= Super class reference   (CORRECT)  - syntactically correct but gives an exception and has to be explicitly caught using try-endtry and catch cx_sy_move_cast_error

If it gives an exception, what is point in using this statement at all? The reference will never be assigned and cannot be used.

Please clarify.

Regards

Vasu

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
821

Hello Vasu,

To understand the concept of "casting" you have to be clear with the concept static & dynamic types for reference variables. In addition to this remember the golden rule of casting:

The dynamic type of a reference variable is always more specific than or the same as the static type

Let us analyse your first 2 statements:

  • Super class reference = Sub class reference --> Golden rule is satisfied. Compiler is happy & no error message
  • Sub class reference = Super class reference --> Golden rule is violated. Compiler complains & throws a syntax error

Now lets see whats's wrong with the 3rd statement

Sub class reference ?= Super class reference

The casting operator (?=) is only an instruction to the compiler to ignore the "golden rule" during static compilation check (in simpler terms the check you do during code generation).

But during runtime the "golden rule" will be checked and if it fails an exception of type CX_SY_MOVE_CAST_ERROR is raised.

A simple example to understand the concept:

PARAMETERS: rb_good RADIOBUTTON GROUP g1,

             rb_bad  RADIOBUTTON GROUP g1.

CLASS lcl_animal DEFINITION.

ENDCLASS.                    "lcl_animal DEFINITION

CLASS lcl_dog DEFINITION INHERITING FROM lcl_animal.

ENDCLASS.                    "lcl_dog DEFINITION

DATA: go_animal   TYPE REF TO lcl_animal,

       go_dog      TYPE REF TO lcl_dog,

       gx_cast_err TYPE REF TO cx_sy_move_cast_error,

       gv_msg      TYPE string.

START-OF-SELECTION.

   IF rb_good = abap_true.

*   Dynamic type of "Animal" refer to "Dog"

     CREATE OBJECT go_animal TYPE lcl_dog.

   ELSEIF rb_bad = abap_true.

*   Dynamic type of animal is same as static type

     CREATE OBJECT go_animal.

   ENDIF.

   TRY .

       go_dog ?= go_animal.

       MESSAGE 'Narrowing cast works!!!' TYPE 'I'.

     CATCH cx_sy_move_cast_error INTO gx_cast_err.

       gv_msg = gx_cast_err->get_text( ).

       MESSAGE gv_msg TYPE 'I'.

   ENDTRY.

Hope this helps.

BR,

Suhas

Message was edited by: Suhas Saha

2 REPLIES 2
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
822

Hello Vasu,

To understand the concept of "casting" you have to be clear with the concept static & dynamic types for reference variables. In addition to this remember the golden rule of casting:

The dynamic type of a reference variable is always more specific than or the same as the static type

Let us analyse your first 2 statements:

  • Super class reference = Sub class reference --> Golden rule is satisfied. Compiler is happy & no error message
  • Sub class reference = Super class reference --> Golden rule is violated. Compiler complains & throws a syntax error

Now lets see whats's wrong with the 3rd statement

Sub class reference ?= Super class reference

The casting operator (?=) is only an instruction to the compiler to ignore the "golden rule" during static compilation check (in simpler terms the check you do during code generation).

But during runtime the "golden rule" will be checked and if it fails an exception of type CX_SY_MOVE_CAST_ERROR is raised.

A simple example to understand the concept:

PARAMETERS: rb_good RADIOBUTTON GROUP g1,

             rb_bad  RADIOBUTTON GROUP g1.

CLASS lcl_animal DEFINITION.

ENDCLASS.                    "lcl_animal DEFINITION

CLASS lcl_dog DEFINITION INHERITING FROM lcl_animal.

ENDCLASS.                    "lcl_dog DEFINITION

DATA: go_animal   TYPE REF TO lcl_animal,

       go_dog      TYPE REF TO lcl_dog,

       gx_cast_err TYPE REF TO cx_sy_move_cast_error,

       gv_msg      TYPE string.

START-OF-SELECTION.

   IF rb_good = abap_true.

*   Dynamic type of "Animal" refer to "Dog"

     CREATE OBJECT go_animal TYPE lcl_dog.

   ELSEIF rb_bad = abap_true.

*   Dynamic type of animal is same as static type

     CREATE OBJECT go_animal.

   ENDIF.

   TRY .

       go_dog ?= go_animal.

       MESSAGE 'Narrowing cast works!!!' TYPE 'I'.

     CATCH cx_sy_move_cast_error INTO gx_cast_err.

       gv_msg = gx_cast_err->get_text( ).

       MESSAGE gv_msg TYPE 'I'.

   ENDTRY.

Hope this helps.

BR,

Suhas

Message was edited by: Suhas Saha

Read only

Former Member
0 Likes
821

Hi,

     subclass reference ?= superclass reference is downcasting. But to do it the superclass reference first be up casted using the same subclass reference else it will give cx_sy_move_cast_error because

A simple rule to remember while down casting.

     Dynamic type of right hand side class reference should be EQUAL OR MORE GENERAL TO Static type of left hand side class reference.

subclass reference ?= superclass reference

This will give error if dynamic type of super class reference is not pointing to the object of same sub class as mentioned on left hand side or of super class.

If super class reference points to object of another sub class then it will give you cx_sy_move_cast_error

Ask me  if any more doubts.

Thank You.