Application Development 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: 

Regarding Narrow casting

Former Member
0 Kudos

Hi Experts,

Here i am giving one program about narrow casting in which i am not able to access the method called 'fasting', but here i am able to access dynamically.

So, while doing narrow casting may i know that only dynamically we can able to access that method?

and may i know the clear picture of wide castening & narrow castening with example.

Please help it out.

REPORT Z15704_NARROWING.

CLASS lcl_animal DEFINITION.

PUBLIC SECTION.

METHODS: hungry.

ENDCLASS.

CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.

PUBLIC SECTION.

METHODS: hungry REDEFINITION,

fasting.

ENDCLASS.

CLASS lcl_animal IMPLEMENTATION.

METHOD hungry.

WRITE: / 'An animal is hungry'.

ENDMETHOD.

endclass.

"hungryENDCLASS.

CLASS lcl_lion IMPLEMENTATION.

METHOD hungry .

WRITE: / 'A Lion (King of Jungle) is hungry.'.

ENDMETHOD. "hungry METHOD fasting.

METHOD fasting.

WRITE: / 'Stop running. Lion is on Fasting today.'.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: lo_animal TYPE REF TO lcl_animal,

lo_lion TYPE REF TO lcl_lion.

" ** ANIMAL object without NARROW casting

WRITE: / 'Animal - without NARROW casting'.

CREATE OBJECT lo_animal.

CALL METHOD lo_animal->hungry( ).

CLEAR lo_animal.

"** ANIMAL object with NARROW CASTING SKIP 2.

WRITE: / 'Animal - NARROW casting from LION'.

CREATE OBJECT lo_lion.

lo_animal = lo_lion.

CALL METHOD lo_animal->hungry( ).

CALL METHOD lo_animal->fasting( ).

when i call this method dynamically CALL METHOD lo_animal->('fasting'). i am able to access.

Thanks,

Radhika

2 REPLIES 2

Former Member
0 Kudos

Hi,

In Widening cast we assign a refernce variable or runtme object of a superclass to that of subclass.

But before doing this, we do narrow casting.

In narrow casting you assign the reference variable of subclass to that of superclass.

Now during widening cast there might be a possibility that

at runtime your superclass variable has the reference of any other its subclasses. But at the same time you are assigning a reference variable to its particular subclass. So there can be a assignment which is incompatible.

thats why

cast error occurs only in

Widening cast.

which is handled by

CX_SY_MOVE_CAST_ERROR

Check this link

I've posted one sample program in link, run this program in debugging mode

Regards

Abhijeet

naimesh_patel
Active Contributor
0 Kudos

In Narrow casting, we create a object reference to the Subclass (LO_LION) first and than we assign that object reference to the reference of the Superclass (LO_ANIMAL). Here we have moved from more specific object to less specific object.

Refer: http://help-abap.blogspot.com/2008/09/abap-objects-narrowing-cast-upcasting.html

In widening cast, we create a object referece to the superclass (LO_ANIMAL) first and than we assign the object reference to the Subclass (LO_LION). Here we move from General object to Specific object. As the principal of the inheritance, every Subobject (LO_LION) will have more attributes/properties than the General object (LO_ANIMAL). So, it would not be possilbe to just create LO_ANIMAL and move to LO_LION. So, when we do the Widening cast, we need to first use the narrow casting somewhere in the program to assign proper proerties of subobject (LO_LION) to Superobject (LO_ANIMAL). Than you will successfully move that reference back to Superobject (LO_ANIMAL) to different subobject (LO_LION2).

Refer: http://help-abap.blogspot.com/2008/09/abap-objects-widening-cast-down-casting.html

Regards,

Naimesh Patel