tags:

views:

254

answers:

1

I'm creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class.

The following code causes a run-time error MOVE_CAST_ERROR:

data: o_child type ref to zchild.

o_child ?= zparent=>provide_instance( ).

I have also tried:

data: o_parent type ref to zparent,
      o_child  type ref to zchild.

o_parent = zparent=>provide_instance( ).
o_child  ?= o_parent.

I have succesfully down-casted with other object types in the past - does anyone have an idea what to look for in ZPARENT or ZCHILD that may make the two classes incompatible?

Currently ZCHILD only has one extra method, which if added to the super class would break the abstraction of the class, so I'm not that keen to do it.

+1  A: 

I didn't do any object oriented coding in abap. Nor much abap programming. But I think that this is a typical oo scenario. So here is my guess.

You can't cast an object to what it ain't.
You have created an instance of the parent class. Now the child class has the property of "myCandy" that the parent class don't have. So your object don't have this property. And then you cast it to child. What the poor run time has to do when you ask for ( or change ) the "myCandy" property? It can do nothing. So it disallow this cast.

The cast is only possible if the object was instantiated as the child and then it was casted to the parent object and then back again to the child object. The child has everything the parent has so there is no problem with this path.

Igal Serban
Thanks - in this case the child had an extra method (I can't remember if there were any new attributes.) I'll have to prove it to myself sometime, but for now I'll accept your word for it :D
Esti