ABAP Objects: Narrowing Cast (Upcasting)

Posted by Krishh Webworld | 11:18 AM | , , | 0 comments »

Important principal of the Inheritence is that an instance of the Subclass can be used in all the contexts where the Superclass can be used. This is possible because the Subclass contains all the attributes of the Superclass, because Subclass has been inhertied from the Super.

When we assign the instance of the Subclass back to the instance of the Superclass, than it is called the "Narrow Casting", because we are switching from a "More Specific view of an object" to "less specific view".
For example: we have a class a global class for ANIMAL. Now, we have inherited more specific class as LION from the ANIMAL. Since LION will have all the attributes from the ANIMAL, we can always refer back to ANIMAL with the reference of the LION.

When this Narrow casting is used?
An application which is interested in the details of the LION, but don't want to know about them. An application only needs to work with the ANIMAL class. In order to allow the application to access the attributes of the LION we have to do a narrow cast and assign it back to the ANIMAL, which is visible from an application.
UML diagram from the example:



Code Snippet for Class ANIMAL & LION
  
*&---------------------------------------------------------------------*
*& This code snippet implements the local calss ANIMAL and LION
*&---------------------------------------------------------------------*
REPORT ztest_narrow_casting.
*
*----------------------------------------------------------------------*
* Animal Super Class defintion
*----------------------------------------------------------------------*
CLASS lcl_animal DEFINITION.
PUBLIC SECTION.
METHODS: hungry.
ENDCLASS. "lcl_animal DEFINITION
*
*----------------------------------------------------------------------*
* Lion subclass defintion

*----------------------------------------------------------------------*
CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.
PUBLIC SECTION.
METHODS: hungry REDEFINITION,
fasting.
ENDCLASS. "lcl_lion DEFINITION
*
*
*----------------------------------------------------------------------*
* Animal Implementation
*----------------------------------------------------------------------*
CLASS lcl_animal IMPLEMENTATION.
METHOD hungry.
WRITE: / 'An animal is hungry'.
ENDMETHOD. "hungry
ENDCLASS. "lcl_animal IMPLEMENTATION
*
*----------------------------------------------------------------------*
* Lion subclass implementation
*----------------------------------------------------------------------*
CLASS lcl_lion IMPLEMENTATION.
METHOD hungry.
WRITE: / 'A Lion (King of Jungle) is hungry.',
'Run as fast as you can..!'.

ENDMETHOD. "hungry
METHOD fasting.
WRITE: / 'Stop running. Lion is on Fasting today.'.
ENDMETHOD.
ENDCLASS. "lcl_lion IMPLEMENTATION


Code Snippet
  
*----------------------------------------------------------------------*
* This code shows how to use the Narrow casting
*----------------------------------------------------------------------*
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 the method FASTING must be a dynamic because FASTING is not
* in the Animal
CALL METHOD lo_animal->('FASTING').


Output from this code snippet:

0 comments

Archives

Subscribe Now: Feed Icon