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

Regarding casting

Former Member
0 Likes
423

Hi Experts,

I have some doubt regarding widening casting? Please refer the below coding.

If i want to move from a less detail view to a more detail view then we r doing widening casting.

>>>>>>

In the below code for widening casting i did as follows,

obj1 = obj2.

obj3 ?= obj1.

Now using obj3 i can access all the methods in the subclass. Then why should i go for the widening casting if i can access the same using the object of subclass(obj2).

>>>>>>>>

if i do the widening casting as follows,

obj3(ref of subclass) ?= obj1(ref of super class)

then i get the runtime error as below.

Runtime Errors OBJECTS_OBJREF_NOT_ASSIGNED

Except. CX_SY_REF_IS_INITIAL

Date and Time 16.02.2007 16:51:49

so to avoid error i have to do casting inside the TRY ..Catch move_cast_error. ...ENDTRY.

So what is the necessities of doing this and in which scenario one should go for this?

Could anyone pls help me in understanding the widening casting concept.????

&----


*& Report ZCL_CASTING

*&

&----


*&

*&

&----


REPORT zcl_casting.

----


  • CLASS cl_demo DEFINITION

----


*

----


CLASS cl_demo DEFINITION.

PUBLIC SECTION.

METHODS: m1, m2, m3.

private section.

methods priv.

ENDCLASS. "cl_demo DEFINITION

----


  • CLASS cl_demo implementation DEFINITION

----


*

----


CLASS cl_demo IMPLEMENTATION.

METHOD m1.

WRITE:/ 'I am in m1 of super'.

ENDMETHOD. "m1

METHOD m2.

WRITE:/ 'I am in m2 of super'.

ENDMETHOD. "m2

METHOD m3.

WRITE:/ 'I am in m3 of super'.

ENDMETHOD. "m3

method priv.

write:/ 'This is a private method'.

endmethod.

ENDCLASS. "cl_demoimplementation DEFINITION

----


  • CLASS cl_sub DEFINITION

----


*

----


CLASS cl_sub DEFINITION INHERITING FROM cl_demo.

PUBLIC SECTION.

METHODS: m4,

m3 REDEFINITION.

ENDCLASS. "cl_sub DEFINITION

----


  • CLASS cl_sub IMPLEMENTATION

----


*

----


CLASS cl_sub IMPLEMENTATION.

METHOD m4.

WRITE:/ 'i am in m4 method of subclass'.

ENDMETHOD. "m4

METHOD m3.

WRITE:/ 'I am in redefined method m3 of subclass'.

ENDMETHOD . "m3

ENDCLASS. "cl_sub IMPLEMENTATION

*******************Main Program*****************

DATA: obj1 TYPE REF TO cl_demo,

obj2 TYPE REF TO cl_sub,

obj3 TYPE REF TO cl_sub.

START-OF-SELECTION.

CREATE OBJECT obj2.

obj1 = obj2.

  • TRY.

obj3 ?= obj1.

  • CATCH cx_sy_move_cast_error.

CALL METHOD obj3->m1.

CALL METHOD obj3->m2.

CALL METHOD obj3->m3.

CALL METHOD obj3->m4.

  • ENDTRY.

2 REPLIES 2
Read only

thomasalexander_ritter
Product and Topic Expert
Product and Topic Expert
0 Likes
334

Your scenario is theoretical and cannot remember seeing stuff like that in my code. So when would I use casting or when should you use casting?

At University I learned that you should always avoid casting because the compiler cannot check during compile time whether it will be possible or not and of course this makes it a lot easier to introduce runtime errors. Those errors can be hard to track down. For me seeing a lot of casting in a program is a code smell. It means that something is wrong with the design. So whenever I have to cast I always check my code and try to solve the problem in a different way. Quite often by using polymorphism you can avoid casting.

Sometimes casting is necessary and especially what people call downcasting. You usually see this kind of stuff in frameworks where a method just returns the type Object. Usually a framework works with an abstract type, like the model class in BSP, if you want to access all methods of this object you need to downcast it to the less abstract type.

But like I said before I avoid casting by any means and only use it as a last resort.

Read only

Former Member
0 Likes
334

Hello Sudhanshu,

Let me first explain why you got the runtime error.

obj3 ?= obj1.

In widening cast operation, target obj1 is superclass object and source obj3 is the subclass object. Since superclass and subclass might have different components,

the current contents of the source (obj1) will not correspond to the type requirements of the target (obj3) and an exception is triggered, if the exception is not handled then the runtime error occurs.

To avoid this, the target obj3 must be either more general or same as the source

obj1. So before widening cast, we do narrowing cast to adjust the source.

obj1 = obj2.

<u><b>Use of Widening Cast</b></u>

A subclass has inherited components from superclass and specific components

of its own. With narrowing cast one can only access the inherited components,

if the requirement is to access the specific components we can do it by using a subclass reference (say obj2) as you had said, but suppose you have already done narrowing cast and passed the superclass reference obj1 to another processing block which does not know the subclass reference. Then in that processing block, to access the specific (and inherited) componets of the subclass we can do widening cast using the superclass reference. It is the availability of <u><b>only</b></u> the superclass reference in some contexts, that makes it necessary to do widening cast to access the specific components of subclass.

Award points if found useful.

Regards

Indrajit