Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Regarding Narrow casting

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

Not what you were looking for? View more on this topic or Ask a question