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: 

Narrowing cast and widening cast

Former Member
0 Kudos
5,026

Hi All,

I want to know what is the use of a narrowing cast and a widening cast in ABAP objects.

Can someone please give an example ?

Regards,

Ashish

1 ACCEPTED SOLUTION

Former Member
0 Kudos
1,193

Hi ,

check this,

cheers,

sowjanya.

7 REPLIES 7

Former Member
0 Kudos
1,193

please refer this thread..

Former Member
0 Kudos
1,193

Hi,

please check these links,

Wideing Cast:

Regards,

Anirban

0 Kudos
1,193

Hi Kishan / Anirban,

Thanks for the reply.

But what i meant was .. I know technically what a narrow or a widening cast is.

I just possibly want a (functional) scenario in which these casting would be used.

Regards,

Ashish

former_member69765
Contributor
0 Kudos
1,193

Since you know the technical funda.. I am giving only example

This is just one of the many examples..

Say you have 2 interfaces IF_1 and IF_2 implemented in class CL_1.

I have an object : DATA : lref_if_1 TYPE REF TO IF_1.

lref_cl_1 TYPE REF TO CL_1.

and I want to access method M1 then I can do it like this ..

CREATE OBJECT CL_1.

lref_if_1 ?= CL_1.

lref_if_1->M1.

Cheers

Former Member
0 Kudos
1,194

Hi ,

check this,

cheers,

sowjanya.

Former Member
0 Kudos
1,193

Hi,

Check out this link, This will guide you on ABAP Objects

http://www.sap-press.de/katalog/buecher/htmlleseproben/gp/htmlprobID-28?GalileoSession=22448306A3l7U...

Both narrow and wide casting are related to inheritance.

In Narrow casting, you assign a runtime object of subclass to runtime object of superclass. By this assignment , you can access your subclass by using runtime object of your superclass. But here the important point is that, you are assigning subclass reference to super class reference, so you can only access the inherited components of subclass through super class reference.

Suppose you have a super class lcl_vehicle. it has two subclasses lcl_car and lcl_truck.

r_vehicle = r_car.

In Wide casting, you assign a superclass reference to subclass reference . Before this, you do narrow casting,

Now when you assign a superclass reference to subclass reference. You can access the inherited as well as the specific components of subclass.

taking the same example,

r_car ?= r_vehicle.

'?=' wide cast operator.

Regards

Abhijeet

Former Member
0 Kudos
1,193

Hi Ashish ,

Hope this link will help you ,

http://www.slideshare.net/srinatha/oo-abap-sap/

Regards,

Sreekar.Kadiri.