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: 

datadic

Former Member
0 Kudos

hi frds plz give me response

what is Upcasting & Downcasting?

give me response to my mail id

sudhakarbabu786@rediffmail.com

3 REPLIES 3

Former Member
0 Kudos

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.

Former Member
0 Kudos

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.

Cheers,

vasavi.

kindly reward if helpful

Former Member
0 Kudos

hi

good

A super class reference can be converted into a subclass reference but this

subclass reference is not useful to call the methods of any of the classes. This convention is known as Downcasting.

We can convert the subclass reference to a superclass reference without using the cast poeration compiler because it will internally take care of the casting. This is known as Upcasting.

-


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.

thanks

mrutyun^