2008 Aug 18 5:29 AM
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
2008 Aug 18 8:07 AM
2008 Aug 18 5:31 AM
2008 Aug 18 5:37 AM
2008 Aug 18 6:09 AM
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
2008 Aug 18 7:07 AM
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
2008 Aug 18 8:07 AM
2008 Aug 18 11:14 AM
Hi,
Check out this link, This will guide you on ABAP Objects
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
2008 Aug 18 12:06 PM
Hi Ashish ,
Hope this link will help you ,
http://www.slideshare.net/srinatha/oo-abap-sap/
Regards,
Sreekar.Kadiri.