‎2008 Jun 02 5:32 AM
Hi Team,
Could any one explain me about Type casting used in oops abap and in what circumstances it is
used with an example.
Regards,
Pradeep P.
‎2008 Jun 02 7:10 AM
Hi,
A cast is performed using the CASTINGaddition of the ASSIGN statement. The CASTINGaddition allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of casting: casting with an implicit type declaration and casting with an explicit type declaration.
Refer this thread below.
Reward Points if Useful.
Rgds,
Anita
‎2008 Jun 02 7:10 AM
Hi,
A cast is performed using the CASTINGaddition of the ASSIGN statement. The CASTINGaddition allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of casting: casting with an implicit type declaration and casting with an explicit type declaration.
Refer this thread below.
Reward Points if Useful.
Rgds,
Anita
‎2008 Jun 02 7:39 AM
yea man 2 sort of casting
1. narrow
2.widen casting..
narrow allows to use redefine method and inherited component of super class..
example is BADI which using this casting because it can make multiple implementation in interface for class....
second is widen which let use redefine method inherited as well new component of subclass.. soo call upcast casting also....
basically this used by interface to dynamically checking relation..
‎2008 Jun 02 8:33 AM
Hi,
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
Reward if helpfull,
Naresh.
‎2020 Sep 18 10:35 AM
Narrow Casting : It’s a process of assigning sub class object to the super class reference it called as Narrow cast.
Wide Casting : It’s a process of assigning super class object to the sub class reference it called as Wide Casting.