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

Narrow Casting Vs Widening Casting

Former Member
0 Likes
870

Hello,

Can anybody please explain me the difference with an example between <b><i>Narrow Casting</i></b> and <i><b>Widening</b></i> <i><b>Casting</b></i> in ABAP Objects.

Regards

Kalyan

2 REPLIES 2
Read only

Lakshmant1
Active Contributor
0 Likes
368
Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
368

Hi Kalyan

In fact, the names imply everything. Let me illustrate a bit. This illustration will deal with one aspect which I think will be enough at least to understand the concept simply. Think of two instances (objects) of two classes one of which is inherited from the other, that is; one is a general class and the other is a specialized class of it.

<u>e.g.</u>

Class 1 --> land_vehicle

Class 2 --> truck

The class <b>land_vehicle</b> has following attributes:


  max_speed
  number_of_wheels
  number_plate
  ...

The class <b>truck</b> has following attributes:


  max_speed [inherited]
  number_of_wheels [inherited]
  number_plate [inherited]
  <i>load_capacity</i> [special to truck class]

Now, assume you have instantiated these classes as

<b>"land_vehicle_obj"</b> and <b>"truck_obj"</b> respectively from these classes.

Between these two objects, you can assign one to the other and at this point casting occurs.

i. If you assign <i>truck_obj</i> to <i>land_vehicle_obj</i> you lose the special attribute <i>load_capacity</i> since you go from the specialized on to the general one. Thus, this is a <b>narrowing cast</b>.

i. If you assign <i>land_vehicle_obj</i> and <i>truck_obj</i> you do not lose any attribute, nevertheless you add a new one. Thus this is a <b>widening cast</b>.

Regards

*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a>