Skip to Content

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

doubt in casting

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

Former Member replied

Hi Lokesh,

Let me explain you about this using another example than your example which would gives you a better understanding on the casting.

The use of casting is to assign the same memory location to both the classes.

Let us assume that there are two classes,

1) Animal ( Class )

2) Dog ( Class )

Dog inherits all properties from the Animal, So Dog is the child class and Animal is the Super class.

If i create the instance of the Animal class and Assigns the same to the Dog, it is called as the Widening casting ( or Down Casting ).

If I do reverse to it... If I create the Dog object and assigns the same to the Animal, this is called as the Narrowing casting ( or Up Casting )

Below example would give you a better idea on casting and it would also answers your question properly.


*----------------------------------------------------------------------*
*       CLASS C1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C1 DEFINITION.
  PUBLIC SECTION.
    data: var1 type ref to object.
    METHODS: METH1.
ENDCLASS.                    "C1 DEFINITION
*----------------------------------------------------------------------*
*       CLASS C1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C1 IMPLEMENTATION.
  METHOD METH1.
    WRITE: / 'This is a method one in class C1'.
  ENDMETHOD.                                                "METH1
ENDCLASS.                    "C1 IMPLEMENTATION
*----------------------------------------------------------------------*
*       CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C2 DEFINITION inheriting from c1.
  PUBLIC SECTION.
    METHODS: METH2, meth1 redefinition.
ENDCLASS.                    "C2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C2 IMPLEMENTATION.
  METHOD METH2.
    WRITE: / 'This is a method two'.
  ENDMETHOD.
   METHOD METH1.
    WRITE: / 'This is a method one in class C2'.
    call method super->meth1.
   ENDMETHOD.
ENDCLASS.                    "C2 IMPLEMENTATION

START-OF-SELECTION.

  DATA REF1 TYPE REF TO C1.
  DATA REF2 TYPE REF TO C2.

  CREATE OBJECT REF2.
  ref1 = ref2.
  ref2 ?= ref1.

  call method ref1->meth1.

Thanks,

Sreekanth

<i> Reward if it helps you</i>

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