Skip to Content
avatar image
Former Member

Widening Cast

Went thru the previous threads on widening cast but it was not helpful

Can someone send me an ABAP program that demonstrates widening class

Kindly reply immediately

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Feb 07, 2007 at 08:26 AM

    Hi Prashanth thanks for ur response.

    I'm clear with narrowing cast.But not clear about widening cast.

    I'm not able to cast the subclass object to a super class object.

    it throws a cast_move_error.

    If Im not able to cast

    sub ?= super then wat is widening cast actually?

    im clear with narrowing cast

    super = sub

    after casting Im able to use the reference of super class to access all the members of the super class ad also the redefined members in the sub class

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Nancy,

      Your understanding of narrow cast is correct.

      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.

      Reward points if this was helpful.

      Rgds,

      Prashanth.

      SAP.

  • avatar image
    Former Member
    Feb 06, 2007 at 04:53 PM

    Hi Nancy,

    I can explain the use of a widening cast for you.

    Imagine, you have a Class B inherting from Class A.

    The class B is now more specific than A, because it has the methods of A, and can also do more things because it has its own methods.

    I have lv_objb type ref to class_b,

    lv_obja type ref to class_a.

    i create the lv_objb as an object of class b.

    But assign this object to lv_obja. This is narrow casting...

    which means I have assigne (or cast) a more speicifc object(B) to a generic object (A).

    But since you are using a generic object, you have only limited access to the object. You can access only those methos that are defined in A. and hence you have a narrower view of the object B in A. An hence the name narrow cast

    The opposite of this is widening cast.

    You have create object B. then you narrow cast by assigning it to a variable of type A.

    Now i assign this object A to B1 which is also of type B.

    So from having a narrow view using A, you have got to a wier view when you assigned to an object B1. hence the name widening cast.

    I am not sure if you are clear now.

    Reward points if it was useful.

    Rgds,

    Prashanth.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2011 at 05:00 AM

    Hi,

    I am quite clear about the usage and the purpose, but i still can't understand why should i go for such a technique when i can use the super keyword.

    For instance, why do i have to go for narrow casting by assigning the sub class object to the super class reference and then access the super class methods. Instead i can just use the super keyword to access. Is this some way of faster accessing using an object reference of a subclass than of a superclass. Please correct my understanding of the idea.

    Thanks

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2012 at 12:28 PM

    *&----------------------------------------------------------------*
    *& Report  ZPMM_CLASS_DYNAMIC                                     *
    *&                                                                *
    *&----------------------------------------------------------------*

    REPORT  ZPMM_CLASS_DYNAMIC                      .

    *----------------------------------------------------------------*
    *       CLASS a DEFINITION
    *----------------------------------------------------------------*
    CLASS SUBI DEFINITION.
       PUBLIC SECTION.
         METHODS: SUBI1,
           SUBI2.
    ENDCLASS.                    "subi DEFINITION

    *----------------------------------------------------------------------*
    *       CLASS subi IMPLEMENTATION
    *----------------------------------------------------------------------*
    *
    *----------------------------------------------------------------------*
    CLASS SUBI IMPLEMENTATION.
       METHOD SUBI1 .
         WRITE :'Subi'.
       ENDMETHOD.                                                "subi1

       METHOD SUBI2.
         WRITE :'Kannottil'.
       ENDMETHOD.                                                "subi2

    ENDCLASS.                    "subi IMPLEMENTATION

    *----------------------------------------------------------------------*
    *       CLASS noufal DEFINITION
    *----------------------------------------------------------------------*
    *
    *----------------------------------------------------------------------*
    CLASS NOUFAL DEFINITION INHERITING FROM SUBI.
       PUBLIC SECTION.
         METHODS : SUBI1 REDEFINITION,
                      NOUFAL1.
    ENDCLASS.                    "noufal DEFINITION

    *----------------------------------------------------------------------*
    *       CLASS noufal IMPLEMENTATION
    *----------------------------------------------------------------------*
    *
    *----------------------------------------------------------------------*
    CLASS NOUFAL IMPLEMENTATION.
       METHOD SUBI1.
         WRITE: / 'Noufal'.
       ENDMETHOD.                                                "subi1

       METHOD NOUFAL1.
         WRITE :'Parappurath'.
       ENDMETHOD.                                                "noufal1
    ENDCLASS.                    "noufal IMPLEMENTATION


    DATA : OBJ1 TYPE REF TO SUBI,
           OBJ2 TYPE REF TO NOUFAL.

    START-OF-SELECTION.

       CREATE OBJECT :    OBJ2 ,OBJ1 .

       OBJ1  =  OBJ2.
       OBJ2 ?=  OBJ1.


       CALL METHOD : OBJ1->SUBI1,
                     OBJ2->SUBI2,
                     OBJ2->SUBI1,
                     OBJ2->NOUFAL1.

    END-OF-SELECTION.

       CALL METHOD : OBJ1->SUBI1,
                     OBJ2->SUBI2,
                     OBJ2->SUBI1,
                     OBJ2->NOUFAL1.

    Add comment
    10|10000 characters needed characters exceeded