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 in ABAP Objects

Former Member
0 Kudos

Why the cast error generates only in Widening cast?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hiii,

shayam if you are not doing the NARROW CASTING before you are performing the WIDE CASTING ,then the cast errors are bound to come...

if you need a detailed description...let me know..

reward if helpful..

thanks..

7 REPLIES 7

Former Member
0 Kudos

Hello,

See this and .

Regards.

Former Member
0 Kudos

HI,

It is possible tthat the static target variable is neithrer more general than nor the same as the dynamic type of the source variable...That is why with this kind of cast the runtime system checks before the assignment wheter the current content of the source variable corresponds to the type requirements of the target variable. Otherwise, an exception that can be handled is triggered and original value of the target variable remains the same.

The exception of the error class is CX_SY_MOVE_CAST_ERROR.

Can be handled in TRY .... CATCH.... block.

The way to prevent this runtime error would be to use runtime tyoe identification(RTTI) classes. They can be used to determine the dynamic type at runtime and to set a condition for the cast.

Hope this would help you.

Regards

Narin Nandivada.

Former Member
0 Kudos

Hi Shyam,

WELCOME TO SDN!!!

Please check this link

http://abapprogramming.blogspot.com/2007/10/oops-abap-8.html

The widening cast in this case does not cause an error because the reference airplane actually points to an instance in the subclass lcl_cargo_airplane. The dynamic type is therefore u2018REF TO lcl_cargo_airplaneu2019.

Here the widening cast produces the MOVE_CAST_ERROR runtime error that can be caught with u201CCATCH ... ENDCATCHu201D, because the airplane reference does not point to an instance in the subclass lcl_cargo_airplane, but to a u201Cgeneral airplane objectu201D. Its dynamic type is therefore u2018REF TO lcl_airplaneu2019 and does not correspond to the reference type cargo_airplane.

The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be checked statically, only at runtime. The Cast Operator u201C?=u201D (or the equivalent u201CMOVE ... ?TO u2026u201D) must be used to make this visible.

With this kind of cast, a check is carried out at runtime to ensure that the current content of the source variable corresponds to the type requirements of the target variables. In this case therefore, it checks that the dynamic type of the source reference airplane is compatible with the static type of the target reference cargo_airplane. If it is, the assignment is carried out. Otherwise the catchable runtime error u201CMOVE_CAST_ERRORu201D occurs, and the original value of the target variable remains the same.

Best regards,

raam

Former Member
0 Kudos

Hi Shyam,

I think there is a confusion. I think your question is about down casting and not wide casting.

Can you please re-confirm.

1. Wider/up casting :

This means a ref object used is of a parent. Now here technically speaking you can only call the methods of the parent class and not of the child class. Even if the parent object is referencing to a child object at runtime, the compiler does not allow you to call the child method.

So there is an error as it can be detected by the compiler and therefore does not have run-time errors.

2. Narrow/down casting :

This means a ref object used is of a child. Now here technically speaking you can call all the methods of the parent class as well as of the child class. So in this case, at runtime if the child object point to a prent object then it will cause an issue if a method specific to a child is called.For the compiler it is not possible to detect this at compile time, like in the above case and therefore a runtime exception is possible.

I hope the above explaination helps you understand this.

Regards,

Saurabh

Former Member
0 Kudos

hiii,

shayam if you are not doing the NARROW CASTING before you are performing the WIDE CASTING ,then the cast errors are bound to come...

if you need a detailed description...let me know..

reward if helpful..

thanks..

0 Kudos

Hii!

In Widening cast we assign a refernce variable or runtme object of a superclass to that of subclass.

But before doing this, we do narrow casting.

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

Now 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 Kulshreshtha

Former Member
0 Kudos

hi shyam

welcome to SDN.

please check this link.

[http://abapprogramming.blogspot.com/2007/10/oops-abap-8.html]

hope this will help.

reward if helpful

Sumit Agarwal