‎2007 Oct 24 7:26 AM
hi sdns,
i am new to OOABAP.I am being trained on this concepts.I am facing a lot of difficulty in understanding Narrowing Cast & Widening Cast.I have some doubts in that.
i have a doubt in widening cast.I Know that we are increasing the scope with the same object in widening cast.
if we have two classes airplane,cargo_airplane
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.
cargo_airplane2 ?= airplane.
if we have a method in airplane.we have a redefined it in cargo_airplane.
After this widening cast.
if we acccess this method from cargo_airplane2.
which method will it access redefined method or the method in super class.
please do explain me this example clearly.
with regards,
lokesh
‎2007 Oct 24 10:57 AM
Hi Lokesh,
In your example it will access the redefined method.
see following code snippet.
class airplane.
method check_fuel_level.
endclass.
class cargo_airplane inherited from airplane.
method check_fuel_level redefined.
method get_cargo_weight.
endclass.
class passenger_airplane inherited from airplane.
method check_fuel_level redefined.
method get_passenger_count().
endclass.
DATA: airplane TYPE REF TO airplane,
cargo_airplane TYPE REF TO cargo_airplane,
cargo_airplane2 TYPE REF TO cargo_airplane,
passenger_airplane TYPE REF TO passanger_airplane.
create object cargo_airplane.
create object passanger_airplane.
* Narrowing Cast
airplane = cargo_airplane.
airplane->check_fuel_level() .
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.
airplane->get_cargo_weight().
" is not allowed as the scope of class airplane does not include the method get_cargo_weight.
* Widening Cast
cargo_airplane2 ?= airplane.
airplane->check_fuel_level().
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.
airplane->get_cargo_weight().
"Will call the method of class cargo_airplaneHence always the object will call the redefined method irrespective of the reference variable type.
If there is no redefinition available in subclass, it will go one level up in inheritance tree to find the next redefinition.
Regards
Geogy
<b>PS: Rewards points if the answer is useful.</b>
‎2007 Oct 24 8:05 AM
Hi Lokesh...
Narrowing Cast: is used for Generalization to Specialization.
Here the Super class reference will access the Instance of a Subclass.
So it can access only the inherited components.
Eg: Calculating the Total fuel for all the Flights (both Cargo and Passenger)
LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref
Total_fuel = total_fuel + <b>WA_AIRPLANE->get_fuel( ). "Calls Redefined methods</b>
ENDLOOP.
Widening Cast: is used for Specialization to Generalization .
Here the Sub class reference will access the Instance of Subclass Which is narrow casted to a Super class reference. that means Widening cast will always use Narrow cast.
Eg: Calculating the Total Cargo capacity for all the Cargo Flights.
data: ref_ex type ref to cx_root.
LOOP AT IT_AIRPLANE INTO WA_AIRPLANE. "Super class ref
TRY .
REF_CARGO ?= WA_AIRPLANE.
Tot_cargo = Tot_cargo + REF_CARGO->get_cargo() . "Calls subclass method
CATCH cx_sy_move_cast_error into ref_ex.
ENDTRY.
ENDLOOP.
<b>Reward if Helpful.</b>
‎2007 Oct 24 10:57 AM
Hi Lokesh,
In your example it will access the redefined method.
see following code snippet.
class airplane.
method check_fuel_level.
endclass.
class cargo_airplane inherited from airplane.
method check_fuel_level redefined.
method get_cargo_weight.
endclass.
class passenger_airplane inherited from airplane.
method check_fuel_level redefined.
method get_passenger_count().
endclass.
DATA: airplane TYPE REF TO airplane,
cargo_airplane TYPE REF TO cargo_airplane,
cargo_airplane2 TYPE REF TO cargo_airplane,
passenger_airplane TYPE REF TO passanger_airplane.
create object cargo_airplane.
create object passanger_airplane.
* Narrowing Cast
airplane = cargo_airplane.
airplane->check_fuel_level() .
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.
airplane->get_cargo_weight().
" is not allowed as the scope of class airplane does not include the method get_cargo_weight.
* Widening Cast
cargo_airplane2 ?= airplane.
airplane->check_fuel_level().
"Will call redefined method of class cargo_airplane.
"if there was no redefinition in cargo_airplane, then it would have called method of class airplane.
airplane->get_cargo_weight().
"Will call the method of class cargo_airplaneHence always the object will call the redefined method irrespective of the reference variable type.
If there is no redefinition available in subclass, it will go one level up in inheritance tree to find the next redefinition.
Regards
Geogy
<b>PS: Rewards points if the answer is useful.</b>
‎2007 Oct 26 7:00 AM