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

downcasting question

Former Member
0 Likes
956

Hi,

Class zlcl_cargo_plane is a subclass of zlcl_airplane. Is the statement r_cargo ?= r_plane in the below code downcasting? What is the purpose of this downcasting? Is it to restrict the determination of highest cargo value to cargo planes only? In debug mode, statement r_cargo ?= r_plane fails if pointer r_plane doesn't point to a cargo plane, and an exception is caught. But I thought that downcasting in this example would restrict the methods available to class zlcl_cargo_plane to those available in class zlcl_airplane.

method GET_HIGHEST_CARGO.

DATA: r_cargo TYPE REF TO zlcl_cargo_plane,

r_plane TYPE REF TO zlcl_airplane,

cargo TYPE s_plan_car,

r_exc TYPE REF TO cx_root.

LOOP AT airplane_list INTO r_plane.

TRY.

r_cargo ?= r_plane. "downcasting u2013 fails if plane is NOT cargo plane,

"and exception cx_sy_move_cast_error is caught

cargo = r_cargo->get_cargo( ).

IF re_cargo < cargo.

re_cargo = cargo.

ENDIF.

CATCH cx_sy_move_cast_error INTO r_exc.

ENDTRY.

ENDLOOP.

endmethod.

Thanks,

John

6 REPLIES 6
Read only

Ramneek
Product and Topic Expert
Product and Topic Expert
0 Likes
835

Hello John,

Is the statement r_cargo ?= r_plane in the below code downcasting? What is the purpose of this downcasting? Is it to restrict the determination of highest cargo value to cargo planes only?

Down-cast (Narrowing Cast) is a technique to assign a super class reference to a sub class reference. There is a switch from a view of a few components to a view of more components. One of the purposes of down casting is to allow you to use the specific components of the subclass instances when these instances are kept in references that are typed on the super class.

Hence, in your example if the instance referred by r_plane is of type Cargo plane you would not be able to directly use the methods / attributes defined in the class Cargo plane. So in order to use the methods / attributes defined in the class Cargo plane you will have to down-cast it to a reference of type Cargo plane. I am assuming that the get_cargo method is defined in the zlcl_cargo_plane class. Hence you would not be able to use this method directly from r_plane. So in order to call the method you will have to down-cast the generic type r_plane to a more specific type r_cargo.

In debug mode, statement r_cargo ?= r_plane fails if pointer r_plane doesn't point to a cargo plane, and an exception is caught.

The run time system checks before making such an assignment if the contents in the source variable (r_plane) correspond to the type requirement of the target variable (r_cargo). If the check fails then an exception is raised. Hence, if the r_plane refers to an instance of some other type of plane then you will get an exception.

But I thought that downcasting in this example would restrict the methods available to class zlcl_cargo_plane to those available in class zlcl_airplane.

This is up-cast. So if you would want to restrict the methods available to class zlcl_cargo_plane to those available in the class zlcl_airplane you have to up-cast it; r_plane = r_cargo. Now only the methods / attributes defined by zlcl_airplane would be available from the reference r_plane.

Hope this helps.

Thank you,

Ramneek

Read only

Former Member
0 Likes
835

Hi Ramneek,

Thank you very much for your thorough answers to my questions. It is very clear to me now.

Best regards,

John

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
835

Hello,

Basic rule of reference variable assigment:

"The static type should be more generic than or equal to the dynamic type of the reference value".

What does this mean?

Static type r_cargo should be more more generic than the dynamic type r_plane in your case. But since r_cargo is the sub-class of r_plane it is less generic than the latter, hence normal assigment cannot be made.

Why we need down cast?

If you try to assign r_plane to r_cargo, the static syntax check will give an error(since basic rule is violated). In this case the casting operator '?=' is used to suppress the static syntax check from throwing the error!

Hope i'm clear with my explanation.

BR,

Suhas

Read only

Former Member
0 Likes
835

Hi Suhas,

Thank you for your help. I really appreciate it.

Best regards,

John

Read only

former_member751136
Discoverer
0 Likes
835

what is the difference between upcasting(narrowing cast) and downcasting( widening cast).

Read only

Former Member
0 Likes
835

This message was moderated.