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: 

ABAP Objects Casting error

Former Member
0 Kudos

Hi,

I have declared two classes with names, ZCL_ONE, ZCL_TWO.

ZCL_TWO is inhering ZCL_ONE.

Now i am using them in the se38 program.

While i use it, Narrow casting is working fine, But widening casting is not working.

It is throwing an exception or a dump if i do not catch an exception.

<i>Code:</i>

DATA: OBJ1 TYPE REF TO ZCL_ONE,
           OBJ2 TYPE REF TO ZCL_TWO.
 CREATE OBJECT OBJ1.
 OBJ2 ?= OBJ1.

The above code is giving me dump. Any guesses about how to solve it.

I am able to work with the Narrow casting.. which is below code.. It is working fine.

DATA: OBJ1 TYPE REF TO ZCL_ONE,
          OBJ2 TYPE REF TO ZCL_TWO.
 CREATE OBJECT OBJ2. 
OBJ1 = OBJ2.

How to resolve the error.

Thanks in Advance.

1 REPLY 1

Former Member
0 Kudos

obj1 should be the super(inherited) class of obj2....while using widening cast....

example:

INTERFACE i1.

DATA a1 TYPE ...

ENDINTERFACE.

INTERFACE i2.

INTERFACES i1.

ALIASES a1 FOR i1~a1.

DATA a2 TYPE ...

ENDINTERFACE.

CLASS c1 DEFINITION.

PUBLIC SECTION.

INTERFACES i2.

ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.

PUBLIC SECTION.

METHODS m1.

ENDCLASS.

...

DATA: iref TYPE REF TO i2,

cref TYPE REF TO c1.

...

CREATE OBJECT iref TYPE c2.

... iref->a1 ...

... iref->a2 ...

...

TRY.

cref ?= iref.

CALL METHOD cref->('M1').

CATCH cx_sy_move_cast_error

cx_sy_dyn_call_illegal_method.

...

ENDTRY.

Message was edited by:

Muthurajan Ramkumar