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: 

casting

Former Member
0 Kudos

can anyone tell me something about casting in object oriented programing

6 REPLIES 6

Former Member
0 Kudos

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

0 Kudos

hi Abhijeet Kulshr...

thankyou for ur kind information.

with regards.

parthu

Former Member
0 Kudos

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..

Former Member
0 Kudos

Hi,

Please refer to the following links:

http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/content.htm

Thanks & regards,

Mayukh

0 Kudos

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

Former Member
0 Kudos

thank u soo much.

with regards.

bhuma.