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

About casting

Former Member
0 Likes
1,892

Hi Any body can u tell me the purpous of casting and tell me the difference b/w narrowing and widening

6 REPLIES 6
Read only

Former Member
0 Likes
1,064

Reward points..

Read only

Former Member
0 Likes
1,064

Hi Venkatesh,,

Hope this Information will be helpful for u..

http://help.sap.com/bestpractices/BBLibrary/HTML/J58_CustConsProc_EN_KO.htm

Regard with points if you find this info helpful.

Regards

Karan

Read only

Former Member
0 Likes
1,064

Hi,

Go to ABAPDOCU tcode and see example programs in abap objects section, you will find separate programs for upcasting and downcasting .

Up-Cast (Widening Cast)

Variables of the type reference to superclass can also refer to subclass instances at runtime.

If you assign a subclass reference to a superclass reference, this ensures that

all components that can be accessed syntactically after the cast assignment are

actually available in the instance. The subclass always contains at least the same

components as the superclass. After all, the name and the signature of redefined

methods are identical.

The user can therefore address the subclass instance in the same way as the

superclass instance. However, he/she is restricted to using only the inherited

components.

In this example, after the assignment, the methods GET_MAKE, GET_COUNT,

DISPLAY_ATTRIBUTES, SET_ATTRIBUTES and ESTIMATE_FUEL of the

instance LCL_TRUCK can only be accessed using the reference R_VEHICLE.

If there are any restrictions regarding visibility, they are left unchanged. It is not

possible to access the specific components from the class LCL_TRUCK of the

instance (GET_CARGO in the above example) using the reference R_VEHICLE.

The view is thus usually narrowed (or at least unchanged). That is why we

describe this type of assignment of reference variables as up-cast. There is a

switch from a view of several components to a view of a few components. As

the target variable can accept more dynamic types in comparison to the source

variable, this assignment is also called Widening Cast

Static and Dynamic Types of References

A reference variable always has two types at runtime: static and dynamic.

In the example, LCL_VEHICLE is the static type of the variable R_VEHICLE.

Depending on the cast assignment, the dynamic type is either LCL_BUS or

LCL_TRUCK. In the ABAP Debugger, the dynamic type is specified in the form

of the following object display.

Down-cast (Narrowing Cast)

Variables of the type “reference to superclass” can also refer to subclass instances

at runtime. You may now want to copy such a reference (back) to a suitable

variable of the type “reference to subclass”.

If you want to assign a superclass reference to a subclass reference, you must

use the down-cast assignment operator MOVE ... ?TO ... or its short form

?=. Otherwise, you would get a message stating that it is not certain that all

components that can be accessed syntactically after the cast assignment are

actually available in the instance. As a rule, the subclass class contains more

components than the superclass.

After assigning this type of reference (back) to a subclass reference to the

implementing class, clients are no longer limited to inherited components: In the

example given here, all components of the LCL_TRUCK instance can be accessed

(again) after the assignment using the reference R_TRUCK2.

The view is thus usually widened (or at least unchanged). That is why we describe

this type of assignment of reference variables as down-cast. There is a switch

from a view of a few components to a view of more components. As the target

variable can accept less dynamic types after the assignment, this assignment is

also called Narrowing Cast

Regards,

Shiva Kumar

Read only

Former Member
0 Likes
1,064

simple language

with narrow casting u can use superclass redefind method and also inherited component.

with wider casting u can access all redefine as well as all componenet of subclass and also inherited component ....

basically known as upcast and downcast..

hope u get help give poins if u can...

Read only

former_member787646
Contributor
0 Likes
1,064

Hi,

In Narrowing Cast the variables that are defined as "reference to super class" can also point to subclass references at program runtime. It is used to execute generic methods of the classes that exist in the same inheritance tree.

In Widening Cast the variables that are defined as "reference to subclass" can also point to superclass references at program runtime. It is used to execute specific method of a class that exist in the same inheritance tree.

Reward if helpful.

Murthy.

Read only

Former Member
0 Likes
1,064

Narrowing Cast is also known as Upcasting...

Widening Cast is known as Downcasting...

Let us take an example...

We define a class lcl_vehicle and other class lcl_truck inherited from lcl_vehicle.

CLASS lcl_vehicle DEFINITION.

PUBLIC SECTION.

METHODS constructor

IMPORTING im_make_v TYPE string.

METHODS estimate_fuel

IMPORTING im_distance TYPE s_distance

RETURNING value(re_fuel) TYPE s_capacity

METHODS get_make........

........

METHODS get_count........

........

METHODS display_attributes........

........

PROTECTED SECTION.

DATA r_tank TYPE REF TO lcl_tank.

PRIVATE SECTION.

DATA make TYPE string.

ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.

METHOD constructor.

make = im_make_v.

ENDMETHOD.

METHOD get_make.

- - -

- - -

ENDMETHOD.

METHOD get_count.

- - -

- - -

ENDMETHOD.

METHOD display_attributes.

- - -

- - -

ENDMETHOD.

ENDCLASS.

Now we define a sub class lcl_truck to super class lcl_vehicle.

CLASS lcl_truck DEFINITION INHERITING FROM lcl_vehicle.

PUBLIC SECTION.

METHODS constructor IMPORTING im_make_t TYPE string

im_cargo_t TYPE s_plan_car

METHODS estimate_fuel REDEFINITION.

METHODS get_cargo RETURNING value( re_cargo ) TYPE s_plan_car.

PRIVATE SECTION.

DATA max_cargo TYPE s_plan_car.

ENDCLASS.

CLASS lcl_truck IMPLEMENTATION.

METHOD constructor.

CALL METHOD super->constructor( im_make_v = im_make_t ).

max_cargo = im_cargo_t.

ENDMETHOD.

METHOD estimete_fuel.

DATA total_weight TYPE w_weight.

total_weight = max_cargo + weight.

re_fuel = total_weight * im_distance * correction_factor.

ENDMETHOD.

ENDCLASS.

Now we again define one more subclass lcl_bus to superclass lcl_vehicle.

CLASS lcl_bus DEFINITION INHERITING FROM lcl_vehicle.

PUBLIC SECTION.

METHODS constructor IMPORTING im_make_b TYPE string

im_load_b TYPE s_plan_car

PRIVATE SECTION.

DATA max_load TYPE s_plan_car.

ENDCLASS.

CLASS lcl_bus IMPLEMENTATION.

METHOD constructor.

CALL METHOD super->constructor( im_make_v = im_make_b ).

max_cargo = im_load_b.

ENDMETHOD.

METHOD estimete_fuel.

DATA total_weight TYPE w_weight.

total_weight = max_passengers * average_weight + weight.

re_fuel = total_weight * im_distance * correction_factor.

ENDMETHOD.

ENDCLASS.

Variables of the type 'reference to superclass' can also refer to subclass instances at runtime.

This is called as Narrowing Casting...

Lets see how it works...

  • Creating references to the classes.........

DATA:

r_vehicle TYPE REF TO lcl_vehicle,

r_truck TYPE REF TO lcl_truck,

r_bus TYPE REF TO lcl_bus.

*Creating an object of class lcl_truck.........

CREATE OBJECT r_truck.

*Narrowing Cast ................

r_vehicle = r_truck.

Now reference --> superclass.... r_vehicle will point to only the inherited compenents of lcl_truck.

If you assign a subclass reference to a superclass reference, this ensures that all components that can be accessed syntactically after the cast assignment are actually available in the instance.

The subclass always contains at least the same components as the superclass. After all, the name and the signature of redefined methods are identical.

The user can, therefore, address the subclass instance in the same way as the superclass instance. However, he is restricted to using only the inherited components. In this example, after the assignment, the methods GET_MAKE, GET_COUNT, DISPLAY_ATTRIBUTES and ESTIMATE_FUEL of the instance LCL_TRUCK can only be accessed using the reference R_VEHICLE. If there are any restrictions regarding visibility, they are left unchanged.

It is not possible to access the specific components from the class LCL_TRUCK of the instance (GET_CARGO in the above example) using the reference R_VEHICLE. The view is thus usually narrowed (or at least unchanged). That is why we describe this type of assignment of reference variables as narrowing cast. There is a switch from a view of several components to a view of a few components. The term upcast is also common.

A typical use for narrowing cast assignments is to prepare for generic access. A user who is not at all interested in the finer points of the instances of the subclasses but who simply wants to address the shared components could use a superclass reference for this access.

In the example shown here, assume that a travel agency (LCL_RENTAL) needs to manage all imaginable kinds of vehicles in one list. This leads to the question of what types should be assigned to the internal table for the references to airplane instances. You should also assume that the car rental company needs to be able to calculate only the required amount of fuel for all its vehicles. Correspondingly, the ESTIMATE_FUEL method is defined in the superclass LCL_VEHICLE and is redefined in all subclasses.

This is about Narrowing Cast.

Inspire if this was needful,

Thanks,

kalyan.