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: 

Narrow casting

Former Member
0 Kudos

Hi,

i done coding for narrow casting but iam not getting proper output.can you please explain the narrow casting using below code.

class c1 definition.

public section.

methods : m1.

endclass.

class c1 implementation.

method m1.

data m1 type i value 2.

write:/ m1.

endmethod.

endclass.

class c2 definition inheriting from c1.

public section .

methods m2.

endclass.

class c2 implementation.

method m2.

data m2 type i value 3.

write:/ m2.

endmethod.

endclass.

start-of-selection.

data obj type ref to c1.

data obj1 type ref to c2.

CREATE object obj1.

obj = obj1.

call method obj1->m2.

Regards,

Suresh

1 ACCEPTED SOLUTION

uwe_schieferstein
Active Contributor
0 Kudos

Hello Suresh

If you want to see a rather weird (yet useful) narrow casting to the root class (OBJECT) you may have a look at my Wiki posting [Accessing the Inacessible - Local Classes within Global Classes|https://wiki.sdn.sap.com/wiki/display/ABAP/AccessingtheInacessible-LocalClasseswithinGlobalClasses]

Regards

Uwe

7 REPLIES 7

naimesh_patel
Active Contributor
0 Kudos

You didn't Cast it properly.

As you can see, we need to call the method M2 dynamically becasue C1 doesn't contain it. So, compiler will give use error.


*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
CLASS C1 DEFINITION.
  PUBLIC SECTION.
    METHODS : M1.
ENDCLASS.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS C1 IMPLEMENTATION.
  METHOD M1.
    DATA M1 TYPE I VALUE 2.
    WRITE:/ M1.
  ENDMETHOD.                    "m1
ENDCLASS.                    "c1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c2 DEFINITION
*----------------------------------------------------------------------*
CLASS C2 DEFINITION INHERITING FROM C1.
  PUBLIC SECTION .
    METHODS M1  REDEFINITION.
    METHODS M2.
ENDCLASS.                    "c2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c2 IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS C2 IMPLEMENTATION.
  METHOD M2.
    DATA M2 TYPE I VALUE 3.
    WRITE:/ M2.
  ENDMETHOD.                    "m2

  METHOD M1.

    WRITE:/ 'M1 from Class C2'.

  ENDMETHOD.                    "m1
ENDCLASS.                    "c2 IMPLEMENTATION

START-OF-SELECTION.
  DATA OBJ TYPE REF TO C1.
  DATA OBJ1 TYPE REF TO C2.
  CREATE OBJECT OBJ1. 
  OBJ ?= OBJ1.     "<<   Casting
*  CALL METHOD OBJ->M2.     " << Error M2 is unknown or Protected
  CALL METHOD OBJ->('M2').   " < Dynamic Access
  CALL METHOD OBJ->M1.

Regards,

Naimesh Patel

Former Member
0 Kudos

Hi,

i think ?= means widening casting if iam wrong please correct me.How will code for narrowcasting?

Regards,

Suresh.

0 Kudos

Oh yes. Sorry I misread the question.

When you do the O1 = O2 (narrow casting) you will only be able to access the methods which are in the class of O2.

Like (from your example):


data obj type ref to c1.
data obj1 type ref to c2.
CREATE object obj1.
obj = obj1.
call method obj1->m2.

Will generate an output of "3", not "2".

Regards,

Naimesh Patel

narin_nandivada3
Active Contributor
0 Kudos

Hi Suresh,

From the above lines..


data obj type ref to c1.
data obj1 type ref to c2.
CREATE object obj1.
obj = obj1.

The OBJ1 can access both the methods M1 and M2 but OBJ can access only M1 method as it is pertaining

to C1 class.

So as you had created the object for Class 2 and as class2 inherits class1 so in short class2 has more methods.

So with the object of Class2 ie OBJ1 you can access two methods.

And now coming to OBJ object you can only access M1 method.

So as you assigning OBJ1 to OBJ object you are making the scope of OBJ narrower ( 'V' shape )

On top of the V think that OBJ1 is there which can access two methods...

But at the bottom of the V there is OBJ which can access only one method and that is method of its own class.

So in this way the Narrow casting Concept can be understood.

So finally you can do

Call method OBJ1->M2.
Call Method OBJ1->M1.
Call Method OBJ->M1.
But not 
Call Method OBJ->M2. " Which is not possible as OBJ doesnot have the scope to Class2

Finally, Narrowing the scope of object is Narrowing Cast and Widening the Scope of the Object is Widening Cast.

Hope this would help you.

Revert back if any questions.

Good luck

Narin

uwe_schieferstein
Active Contributor
0 Kudos

Hello Suresh

If you want to see a rather weird (yet useful) narrow casting to the root class (OBJECT) you may have a look at my Wiki posting [Accessing the Inacessible - Local Classes within Global Classes|https://wiki.sdn.sap.com/wiki/display/ABAP/AccessingtheInacessible-LocalClasseswithinGlobalClasses]

Regards

Uwe

Former Member
0 Kudos

Below iam using narrow casting code but i able to access m2 method in obj.

----


  • CLASS c1 DEFINITION

----


CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS : M1.

ENDCLASS. "c1 DEFINITION

----


  • CLASS c1 IMPLEMENTATION

----


CLASS C1 IMPLEMENTATION.

METHOD M1.

DATA M1 TYPE I VALUE 2.

WRITE:/ M1.

ENDMETHOD. "m1

ENDCLASS. "c1 IMPLEMENTATION

----


  • CLASS c2 DEFINITION

----


CLASS C2 DEFINITION INHERITING FROM C1.

PUBLIC SECTION .

METHODS M1 REDEFINITION.

METHODS M2.

ENDCLASS. "c2 DEFINITION

----


  • CLASS c2 IMPLEMENTATION

----


CLASS C2 IMPLEMENTATION.

METHOD M2.

DATA M2 TYPE I VALUE 3.

WRITE:/ M2.

ENDMETHOD. "m2

METHOD M1.

WRITE:/ 'M1 from Class C2'.

ENDMETHOD. "m1

ENDCLASS. "c2 IMPLEMENTATION

START-OF-SELECTION.

DATA OBJ TYPE REF TO C1.

DATA OBJ1 TYPE REF TO C2.

CREATE OBJECT OBJ1.

OBJ = OBJ1. "<< Casting

CALL METHOD OBJ->('M2'). " < Dynamic Access

CALL METHOD OBJ->M1.

if iam using OBJ ?= OBJ1(widening casting) in code also iam getting same output . can you Please explain me clearly narrow casting and widening casting using my classess example.

Regards,

Suresh.

Former Member
0 Kudos

Hi,

Thanks for replies.

Regards,

Suresh