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

Narrow Casting

Former Member
0 Likes
2,130

Hi all,

Can anybody please tell the usuage of narrow casting.

preferable with simple code.

Anirban Bhattacharjee

1 ACCEPTED SOLUTION
Read only

former_member188594
Active Participant
0 Likes
1,919

Hi,

Narrowing Cast

If the static type of the target variable is less specific or the same as the static type of the source variable, assignment is always possible. In comparison to the source variable, the target variable knows the same or fewer attributes of the dynamic type. As a result, this assignment is referred to as narrowing cast. A narrowing cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. These include, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or the transfer from actual to formal parameters.

This means a ref object used is of a child. Now here technically speaking you can call all the methods of the parent class as well as of the child class. So in this case, at runtime if the child object point to a prent object then it will cause an issue if a method specific to a child is called.For the compiler it is not possible to detect this at compile time, like in the above case and therefore a runtime exception is possible.

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

Reward points if useful.

Best Regards,

Sekhar

3 REPLIES 3
Read only

former_member188594
Active Participant
0 Likes
1,920

Hi,

Narrowing Cast

If the static type of the target variable is less specific or the same as the static type of the source variable, assignment is always possible. In comparison to the source variable, the target variable knows the same or fewer attributes of the dynamic type. As a result, this assignment is referred to as narrowing cast. A narrowing cast is possible in all ABAP statements in which the content of a data object is assigned to another data object. These include, for example, assignments with the normal assignment operator (=), the insertion of rows in internal tables, or the transfer from actual to formal parameters.

This means a ref object used is of a child. Now here technically speaking you can call all the methods of the parent class as well as of the child class. So in this case, at runtime if the child object point to a prent object then it will cause an issue if a method specific to a child is called.For the compiler it is not possible to detect this at compile time, like in the above case and therefore a runtime exception is possible.

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

Reward points if useful.

Best Regards,

Sekhar

Read only

Former Member
0 Likes
1,919

2. Narrow/down casting :

This means a ref object used is of a child. Now, technically speaking you can call all the methods of the parent class as well as of the child class due to inheritance. Typically you need narrow casting whenever you do wider casting. This is because after wider casting it is not possible to call child class specific methods.

For eg.

Class p1

{

method x.

}

Class c1 parent p1

{

method y. (this class also has method x due to inheritance)

}

Now.

DATA : p type ref to p1. -


> here p is a ref to parent object.

c type ref to c1 -


> here c is a ref to child object.

DATA : obj type ref to c1.

CREATE OBJECT obj.

p = obj. -


> this is wider casting.

In thi case it is not possible to call method y as p is a reference to a parent object.

So let us say now if you want to call the method y then you can do the following.

c ?= p. -


> this is narrow casting.

c-->y. -


> call to method y is possible.

I hope this helps you. Be careful, narrow casting can lead to run time errors if the object assigned to reference does not have the right scope.

Regards,

Saurabh

Read only

Former Member
0 Likes
1,919

Thanks all.

I hav got the concept