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

cast in oops

Former Member
0 Likes
894

what is cast? what is narrowing cast?what is widening cast?what is ME? plz explai me with good examples.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
700

Hi,

Narrowing” cast means that the assignment changes from a more specialized view (with visibility to more components) to a more generalized view (with visibility to fewer components).

“Narrowing cast” is also referred to as “up cast” . “Up cast” means that the static type of the target variable can only change to higher nodes from the static type of the source variable in the inheritance tree, but not vice versa.

Reference variable of a class assigned to reference variable of class : object

class c1 definition.

endclass.

class c1 implementation.

endclass.

class c2 definition inheriting from c1.

endclass.

class c2 implementation.

endclass.

start-of-selection.

data : oref1 type ref to c1,

oref2 tyep ref to c2.

oref1 = oref2.[/code]

Widening Cast

DATA: o_ref1 TYPE REF TO object, o_ref2 TYPE REF TO class.

...

o_ref2 ?= o_ref1.

...

CATH SYSTEM-EXCEPTIONS move_cast_error = 4.

o_ref2 ?= o_ref1.

ENDCATCH.[/code]In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast.

The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable.

However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator “?=“ and catch the potential runtime error move_cast_error.

For widening cast, you are partially correc that you do a sub ?= super.

But the pre-condition is that the super should be pointing to a object of sub.

which means you have done a naroow cast from sub to super earlier (Narrow cast). And now you want to widen the cast back to the sub object.

So eg could be..

sub1 type ref to sub_cls,

sub2 type ref to sub_cls,

super type ref to super_cls. "this is just example. u cant use super as the object name.

now you do super = sub1. "narrow cast

sub2 = super. "widen the cast.

As I said, wide casting is possible, only when it has an object of the sub-object but the object is typed to a super class.

Hope you have now understo the reason why you are getting the error.

Regards,

Raj.

3 REPLIES 3
Read only

Former Member
0 Likes
701

Hi,

Narrowing” cast means that the assignment changes from a more specialized view (with visibility to more components) to a more generalized view (with visibility to fewer components).

“Narrowing cast” is also referred to as “up cast” . “Up cast” means that the static type of the target variable can only change to higher nodes from the static type of the source variable in the inheritance tree, but not vice versa.

Reference variable of a class assigned to reference variable of class : object

class c1 definition.

endclass.

class c1 implementation.

endclass.

class c2 definition inheriting from c1.

endclass.

class c2 implementation.

endclass.

start-of-selection.

data : oref1 type ref to c1,

oref2 tyep ref to c2.

oref1 = oref2.[/code]

Widening Cast

DATA: o_ref1 TYPE REF TO object, o_ref2 TYPE REF TO class.

...

o_ref2 ?= o_ref1.

...

CATH SYSTEM-EXCEPTIONS move_cast_error = 4.

o_ref2 ?= o_ref1.

ENDCATCH.[/code]In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast.

The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable.

However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator “?=“ and catch the potential runtime error move_cast_error.

For widening cast, you are partially correc that you do a sub ?= super.

But the pre-condition is that the super should be pointing to a object of sub.

which means you have done a naroow cast from sub to super earlier (Narrow cast). And now you want to widen the cast back to the sub object.

So eg could be..

sub1 type ref to sub_cls,

sub2 type ref to sub_cls,

super type ref to super_cls. "this is just example. u cant use super as the object name.

now you do super = sub1. "narrow cast

sub2 = super. "widen the cast.

As I said, wide casting is possible, only when it has an object of the sub-object but the object is typed to a super class.

Hope you have now understo the reason why you are getting the error.

Regards,

Raj.

Read only

Former Member
Read only

Former Member
0 Likes
700

NARROWING CAST

One of the significant principles of inheritance is that an instance from a subclass can be used in every

context in which an instance from the superclass appears. This is possible because the subclass has

inherited all components from the superclass and therefore has the same interface as the superclass. The

user can therefore address the subclass instance in the same way as the superclass instance.

Variables of the type “reference to superclass” can also refer to subclass instances at runtime.

The assignment of a subclass instance to a reference variable of the type “reference to superclass” is

described as a narrowing cast, because you are switching from a view with more detail to a view with less

detail.

The description “up-cast” is also used.

What is a narrowing cast used for? A user who is not interested in the finer points of cargo or passenger

planes (but only, for example, in the tank gauge) does not need to know about them. This user only needs

to work with (references to) the lcl_airplane class. However, in order to allow the user to work with cargo or

passenger planes, you would normally need a narrowing cast.

example




DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
level TYPE ty_level.
CREATE OBJECT cargo_airplane.
* Subclass instance understands the same messages
* as superclass instance
CALL METHOD cargo_airplane->get_fuel_level RECEIVING re_level = level.
* Narrowing Cast
airplane = cargo_airplane.
* Using the subclass instance in the superclass context
CALL METHOD airplane->get_fuel_level RECEIVING re_level = level.

After the narrowing cast you can use the airplane reference to access the components of the cargo plane

instance that were inherited from lcl_airplane, obviously in some cases with the limitations entailed by their

visibility. You can no longer access the cargo-plane-specific part of the instance (cargo in the above

example) using the airplane reference.

A reference variable always has two types: static and dynamic:

- The static type of a reference variable is determined by variable definition using TYPE REF TO. It cannot

and does not change. It establishes which attributes and methods can be addressed

- The dynamic type of a reference variable is the type of the instance currently being referred to, it is

therefore determined by assignment and can change during the program run. It establishes what coding is

to be executed for redefined methods.

In the example, the static type of the airplane variable is always ‘REF TO lcl_airplane’, but its dynamic type

after the cast is ‘REF TO lcl_cargo_airplane’.

The reference ME can be used to determine the dynamic type in the Debugger.

imp

The static type of a reference variable

Is determined using TYPE REF TO

Remains constant throughout the program run

Establishes which attributes and methods can be

addressed

The dynamic type of a reference variable

Is determined by assignment

Can change during the program run

Establishes what coding is to be executed for redefined

methods

WIDENING CAST

The type of case described above is known as a widening cast because it changes the view to one with

more details. The instance assigned (a cargo plane in the above example) must correspond to the object

reference (cargo_airplane in the above example), that is, the instance must have the details implied by the

reference.

This is also known as a “down cast”.

The widening cast in this case does not cause an error because the reference airplane actually points to an

instance in the subclass lcl_cargo_airplane. The dynamic type is therefore ‘REF TO lcl_cargo_airplane’.

EXAMPLE


DATA: airplane TYPE REF TO lcl_airplane,
cargo_airplane TYPE REF TO lcl_cargo_airplane,
cargo_airplane2 TYPE REF TO lcl_cargo_airplane.

CREATE OBJECT cargo_airplane.
airplane = cargo_airplane.

imp about widening cast

Cannot be checked statically

If unsuccessful, ends with a catchable runtime




CATCH SYSTEM-EXCEPTION MOVE_CAST_ERROR = 4.
cargo_airplane ?= airplane.
ENDCATCH.
IF SY-SUBRC EQ 4.
...
ENDIF.


The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be

checked statically, only at runtime. The Cast Operator “?=” (or the equivalent “MOVE ... ?TO …”) must be

used to make this visible.

With this kind of cast, a check is carried out at runtime to ensure that the current content of the source

variable corresponds to the type requirements of the target variables. In this case therefore, it checks that

the dynamic type of the source reference airplane is compatible with the static type of the target reference

cargo_airplane. If it is, the assignment is carried out. Otherwise the catchable runtime error

“MOVE_CAST_ERROR” occurs, and the original value of the target variable remains the same.

cheers

sharad

Edited by: sharad narayan on Apr 16, 2008 1:33 PM