Application Development 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: 

Widening Cast

andy_dingfelder3
Participant
0 Kudos

Hi,

i´m completly confused concerning the widening cast. The following example example won´t work. But why?

Class ZCL_BMW is a subclass of class ZCL_CAR so I though that the widening cast from CAR to BMW will work....

Any ideas, whats wrong in this example?

Regards Andy

REPORT  ztest_widening_cast.

DATA: lr_car TYPE REF TO zcl_car,
      lr_bmw TYPE REF TO zcl_bmw.

CREATE OBJECT lr_car.

CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
  lr_bmw ?= lr_car.
ENDCATCH.

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

It would not work, because this code will not set all the attributes of the LR_BMW and which will make the object incomplete and it will give you casting error.


  LR_BMW ?= LR_CAR

Try to use the TRY .. CATCH.. ENDTRY block instead of the obsolute syntax CATCH .. ENDCATCH.


DATA: lr_car TYPE REF TO zcl_car,
      lr_bmw TYPE REF TO zcl_bmw,
      lr_merc TYPE REF TO zcl_bmw,  " also subclass of the ZCL_CAR
      lo_cast_error TYPE REF TO cx_sy_move_cast_error.

* widening cast
  CREATE OBJECT lr_car.
  TRY.
       lr_bmw ?= lr_car.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE: / 'Widening cast failed 1'.
  ENDTRY.

  CREATE OBJECT lr_merc.
  lr_car = lr_merc.  " all the Attributes of the ZCL_BMW
  TRY.
       lr_bmw ?= lr_car.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE: / 'Widening cast failed 2'.
  ENDTRY.

You might want to check this out: [Widening Cast|http://help-abap.blogspot.com/2008/09/abap-objects-widening-cast-down-casting.html]

Regards,

Naimesh Patel

2 REPLIES 2

naimesh_patel
Active Contributor
0 Kudos

It would not work, because this code will not set all the attributes of the LR_BMW and which will make the object incomplete and it will give you casting error.


  LR_BMW ?= LR_CAR

Try to use the TRY .. CATCH.. ENDTRY block instead of the obsolute syntax CATCH .. ENDCATCH.


DATA: lr_car TYPE REF TO zcl_car,
      lr_bmw TYPE REF TO zcl_bmw,
      lr_merc TYPE REF TO zcl_bmw,  " also subclass of the ZCL_CAR
      lo_cast_error TYPE REF TO cx_sy_move_cast_error.

* widening cast
  CREATE OBJECT lr_car.
  TRY.
       lr_bmw ?= lr_car.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE: / 'Widening cast failed 1'.
  ENDTRY.

  CREATE OBJECT lr_merc.
  lr_car = lr_merc.  " all the Attributes of the ZCL_BMW
  TRY.
       lr_bmw ?= lr_car.
    CATCH cx_sy_move_cast_error INTO lo_cast_error.
      WRITE: / 'Widening cast failed 2'.
  ENDTRY.

You might want to check this out: [Widening Cast|http://help-abap.blogspot.com/2008/09/abap-objects-widening-cast-down-casting.html]

Regards,

Naimesh Patel

0 Kudos

> It would not work, because this code will not set all the attributes of the LR_BMW and which will make the object incomplete and it will give you casting error.

>


>   LR_BMW ?= LR_CAR
> 

>

> Try to use the TRY .. CATCH.. ENDTRY block instead of the obsolute syntax CATCH .. ENDCATCH.

>


> DATA: lr_car TYPE REF TO zcl_car,
>       lr_bmw TYPE REF TO zcl_bmw,
>       lr_merc TYPE REF TO zcl_bmw,  " also subclass of the ZCL_CAR
>       lo_cast_error TYPE REF TO cx_sy_move_cast_error.
> 
> * widening cast
>   CREATE OBJECT lr_car.
>   TRY.
>        lr_bmw ?= lr_car.
>     CATCH cx_sy_move_cast_error INTO lo_cast_error.
>       WRITE: / 'Widening cast failed 1'.
>   ENDTRY.
> 
>   CREATE OBJECT lr_merc.
>   lr_car = lr_merc.  " all the Attributes of the ZCL_BMW
>   TRY.
>        lr_bmw ?= lr_car.
>     CATCH cx_sy_move_cast_error INTO lo_cast_error.
>       WRITE: / 'Widening cast failed 2'.
>   ENDTRY.
> 

>

> You might want to check this out: Widening Cast

>

> Regards,

> Naimesh Patel

Hi,

thanks for your detailed answer. It seems that I misunderstand the princip of widening cast. But thanks to your answer it´s clear to me now!.

Cheers,

Andy