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: 

Casting Help

Former Member
0 Kudos

Hi,

i learn OO and maybe someone can explain me

what is the action in bold is doing ?

r_cargo ?= r_plane.

Regards

DATA: r_cargo TYPE REF TO lcl_cargo_plane.

DATA: r_plane TYPE REF TO lcl_airplane.

LOOP AT airplane_list INTO r_plane.

TRY.

r_cargo ?= r_plane.

cargo = r_cargo->get_cargo( ).

IF re_cargo < cargo.

re_cargo = cargo.

ENDIF.

CATCH cx_sy_move_cast_error INTO r_exc.

ENDTRY

1 ACCEPTED SOLUTION

narin_nandivada3
Active Contributor
0 Kudos

Hi,

Its nothing but Wide casting Concept in OOABAP.

In short you are increasing the scope of accessibility of that object.

*Lets say there are two classes CLASS A and CLASS B.*

*Class B inherits CLASS A..*

*So you had created object for CLASS A and you had just delcared object for CLASS B.*
*So now to increase the scope of the CLASS B object we can use Wide-casting concept..*

*DATA:*
  *W_ref_A type REF TO CLASS A,*
  *W_REF_B type REF TO CLASS B.*

*Create object CLASS A.*
*-------------------------*
*After some logic (optional)*
*-------------------------*

*W_REF_B ?= W_REF_A.  " Wide casting..*

*By Wide-casting the scope increases like with W_REF_A you cannot access the other methods define* 
*in class B other than inherited methods but using W_REF_B you can access as it is reference to Class B.*

Check these threads for more details..

Hope this would solve your issue.

Good luck

Narin

Edited by: Narin Nandivada on Sep 8, 2008 3:26 PM

6 REPLIES 6

narin_nandivada3
Active Contributor
0 Kudos

Hi,

Its nothing but Wide casting Concept in OOABAP.

In short you are increasing the scope of accessibility of that object.

*Lets say there are two classes CLASS A and CLASS B.*

*Class B inherits CLASS A..*

*So you had created object for CLASS A and you had just delcared object for CLASS B.*
*So now to increase the scope of the CLASS B object we can use Wide-casting concept..*

*DATA:*
  *W_ref_A type REF TO CLASS A,*
  *W_REF_B type REF TO CLASS B.*

*Create object CLASS A.*
*-------------------------*
*After some logic (optional)*
*-------------------------*

*W_REF_B ?= W_REF_A.  " Wide casting..*

*By Wide-casting the scope increases like with W_REF_A you cannot access the other methods define* 
*in class B other than inherited methods but using W_REF_B you can access as it is reference to Class B.*

Check these threads for more details..

Hope this would solve your issue.

Good luck

Narin

Edited by: Narin Nandivada on Sep 8, 2008 3:26 PM

0 Kudos

Hi Narin,

Thanks but maybe u can explain me this code:

cl_abap_typedescr is class interface

DATA: gx_struc TYPE REF TO cl_abap_structdescr.
  DATA: gx_table TYPE REF TO cl_abap_tabledescr.

  gx_table ?= cl_abap_typedescr=>describe_by_data( <col_actual> )."--Get Table Colman Numbers--*
  gx_struc ?= gx_table->get_table_line_type( ).
  DESCRIBE TABLE gx_struc->components LINES t_lines.

Regards

0 Kudos

Hi,

As CL_ABAP_TYPEDESCR is an Abstract class having the static method as DESCRIBE_BY_DATA.

and its RETURNING parameter is typed as a reference to this superclass. since the actual parameter

gx_table is typed on the subclass CL_ABAP_STRUCTDESCR, so the object has to be assigned using

Widening Cast with operator ?= .

Please check this document which would give your more clear idea...

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/f2dac69e-0e01-0010-e2b6-81c1e8e5...

Hope this would help you.

Good luck

Narin

Former Member
0 Kudos

?= is Wide casting or down cast

narin_nandivada3
Active Contributor
0 Kudos

Hi,

for additional information please check this thread

Check this link

http://www.abapprogramming.net/2007/10/oops-abap-8.html

Hope this would help you.

Good luck

Narin

Former Member
0 Kudos

Hi,

'?=' This is widening cast operator. in your example you have assigned instance of superclass to instance of subclass.

After this assignment, you can access the specific components of subclass lcl_cargo_plane using instance r_cargo.

But before doing this, we do narrow casting.

In narrow casting you assign the reference variable of subclass to that of superclass.

I would like to add one more point that

during widening cast there might be a possibility that

at runtime your superclass variable has the reference of any other its subclasses. But at the same time you are assigning a reference variable to its particular subclass. So there can be a assignment which is incompatible.

thats why

cast error occurs only in

Widening cast.

which is handled by

CX_SY_MOVE_CAST_ERROR

Regards

Abhijeet