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

Example for Widening Cast

itabhishek9
Participant
0 Likes
835

Hello SDNites,

Can someone give me a simple example for widening cast. I am not able to implement the same. Also please tell me the scenarion where the same will be used.

Regards,

Abhi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
737

Hi Abhishek,

A widening cast is when you declare a reference to the superclass and then copy a subclass reference into it.

For eg: You have vehicle class with subclasses like car, truck, bus etc.

You would have implemented the common attributes and methods in the vehicle class.You want to call a method that has been redefined in each of the subclasses. You can then create an internal table with type reference to the vehicle class and fill it up with all the subclass references. Then the redefined method of each subclass can be individually called using the vehicle class reference.

The widening cast operator is ?=.

Check this link to get a better picture.

http://help.sap.com/abapdocu_70/en/ABAPMOVE.htm

Hope this has given you an idea.

Nandesh

3 REPLIES 3
Read only

Former Member
0 Likes
737
Read only

Sm1tje
Active Contributor
0 Likes
737

Well, here goes THAT discussion again. There has been some confusion on this in the past (search SCN for example);

Widening cast = Up Cast

Narrowing cast = Down Cast

This is what is says in the current version of the ABAP Help (SAP_ABA 7.01 SP 3):

down cast

Also called a narrowing cast, a down cast is an assignment between reference variables in which the typr static type of the target variable more specific than the static type of the source variable. A down cast is only possible in assignments with the casting operator (?=) or MOVE ... ?TO.

Also referred to as widening cast. Assignment between reference variables, where the static type of the target variables is similar or identical to the static type of the source variables. See Down Cast.

So now the relevant question would be: Are you actually looking for a narrowing of widening cast (real life) example?

Read only

Former Member
0 Likes
738

Hi Abhishek,

A widening cast is when you declare a reference to the superclass and then copy a subclass reference into it.

For eg: You have vehicle class with subclasses like car, truck, bus etc.

You would have implemented the common attributes and methods in the vehicle class.You want to call a method that has been redefined in each of the subclasses. You can then create an internal table with type reference to the vehicle class and fill it up with all the subclass references. Then the redefined method of each subclass can be individually called using the vehicle class reference.

The widening cast operator is ?=.

Check this link to get a better picture.

http://help.sap.com/abapdocu_70/en/ABAPMOVE.htm

Hope this has given you an idea.

Nandesh