08-04-2008 1:51 PM
can anyone tell me something about casting in object oriented programing
08-04-2008 1:59 PM
Hi,
Casting in ABAP OBJECTS is concept related to inheritance.
Narrow Casting: When you assign reference of subclass to reference of superlass, then by superclass reference, you can access the inhertied components of subclass.
Suppose you have a class, lcl_vehicle(reference r_vehicle)
and its subclass lcl_car(r_car).
you have a reference variable:
just have a look at example of narrow casting
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_car TYPE REF TO lcl_car.
CREATE OBJECT r_car.
r_vehicle = r_car
Wide Casting: When you assign a superclass reference to subclass reference after narrow casting. Now with this you can access the specific components of subclass.
Have a look at this example
DATA: r_vehicle TYPE REF TO lcl_vehicle,
r_car TYPE REF TO lcl_car,
r_car2 LIKE r_car.
CREATE OBJECT r_car.
r_vehicle = r_car.
r_car2 ?= r_vehicle.
'?=' This is wide cast operator.
Now with r_car2 reference you can access inherited as well as specific components of subclass.
Check these links
Regards
Abhijeet
08-05-2008 3:41 PM
hi Abhijeet Kulshr...
thankyou for ur kind information.
with regards.
parthu
08-04-2008 2:09 PM
hiii...
there are 2 casting in oops concept.
narrow casting and wide casting..
Imagine, you have a Class B inherting from Class A.
The class B is now more specific than A, because it has the methods of A, and can also do more things because it has its own methods.
I have lv_objb type ref to class_b,
lv_obja type ref to class_a.
i create the lv_objb as an object of class b.
But assign this object to lv_obja. This is narrow casting...
which means I have assigne (or cast) a more speicifc object(B) to a generic object (A).
But since you are using a generic object, you have only limited access to the object. You can access only those methos that are defined in A. and hence you have a narrower view of the object B in A. An hence the name narrow cast
The opposite of this is widening cast.
You have create object B. then you narrow cast by assigning it to a variable of type A.
Now i assign this object A to B1 which is also of type B.
So from having a narrow view using A, you have got to a wier view when you assigned to an object B1. hence the name widening cast.
hopefully it will help you a bit...
thanks..
08-04-2008 2:12 PM
Hi,
Please refer to the following links:
http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/content.htm
Thanks & regards,
Mayukh
08-04-2008 3:07 PM
Casting, in the simplest definition, is assigning of an object of some specific datatype to a variable of some other (technically different, but compatible) datatype.
Keeping this picture in mind, you can re-read and explore what others have already provided in this thread.
-- Zakir
08-05-2008 4:05 PM