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

Inheritance

Former Member
0 Likes
487

What is narrowing cast and widening cast in case of inheritance.Please let me know with help of example.

3 REPLIES 3
Read only

Former Member
0 Likes
436

Hi,

When you assign a data object to a field symbol you can cast to any data type. This means that any area of memory can be considered to have a assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing.

Check the below link which has a example of both narrow and wide casting:

[http://www.abap2java.com/Casting#Implicit_Casting_.28Widening_Cast.29] --- has example.

Please check the below thread which can help you further : it has the explantion.

[;

Check this too.

[http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/content.htm]

Reward if useful,

Sravani.

Read only

Shafiq_Rehman1
Active Contributor
0 Likes
436

A cast can be used to narrow the type of a reference--to make it more specific. Often, we'll do this when we have to retrieve an object from a more general type of collection or when it has been previously used as a less derived type.

Narrow Casting Example in Java:

Animal creature = ...

Cat simon = ...

creature = simon; // Okay

// simon = creature; // Compile time error, incompatible type

simon = (Cat)creature; // Okay

reference: http://www.opensitesolutions.com/books/comp_books/ora/exp/ch06_01.htm

Read only

Former Member
0 Likes
436

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,

vasavi.

kindly reward if helpful.