Application Development and Automation 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: 
Read only

Wide casting problem

sreenivas_pachva
Participant
0 Likes
1,052

Hi All,

Can u explain Wide casting in detail ? already i gone through sdn links but still i didnt get clear . please go through the follwing code snippet and explain where i made mistake in order to getting wide casting.. my problem is as wide casting i want to access the base class methods with reference of sub class when the base class methods are redefined in sub class(actually sub class reference access the subclass methods only if we have same methods in base and as well as sub class) in my program after performing wide cast also it is accessing sub class methods only please send the answer.

REPORT  ZNARROW_WIDE.

CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS:M1,M2.

ENDCLASS.

CLASS C1 IMPLEMENTATION.

METHOD M1.

WRITE:/ ' THIS IS SUPER CLASS METHOD M1'.

ENDMETHOD.

METHOD M2.

WRITE:/ ' THIS IS SUPER CLASS METHOD M2'.

ENDMETHOD.
ENDCLASS.

CLASS C2 DEFINITION INHERITING FROM C1.
PUBLIC SECTION.
METHODS:M1 REDEFINITION.
METHODS:M2 REDEFINITION,
                  M3.

ENDCLASS.


CLASS C2 IMPLEMENTATION.

METHOD M1.

WRITE:/ ' THIS IS SUB CLASS METHOD M1'.
ENDMETHOD.


METHOD M2.

WRITE:/ ' THIS IS SUBCLASS METHOD M2'.
ENDMETHOD.


METHOD M3.

WRITE:/ ' THIS IS SUB CLASS METHOD M3'.
ENDMETHOD.



ENDCLASS.


DATA:O_SUPER TYPE REF TO C1,
     O_SUB TYPE REF TO C2.


START-OF-SELECTION.


CREATE OBJECT O_SUPER.

CREATE OBJECT O_SUB.




CALL METHOD O_SUPER->M1.

CALL METHOD O_SUPER->M2.

CLEAR O_SUPER.

O_SUPER = O_SUB.

CALL METHOD O_SUPER->('M3').


SKIP 5.

ULINE 1(80).
CLEAR O_SUB.


O_SUB ?= O_SUPER.


CALL METHOD O_SUB->M1.

CALL METHOD O_SUB->M2.

CALL METHOD O_SUB->M3.

Thanks in advance.

sreenivas P

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
982

Hi Srinivas,

I figured out what your problem was.

From your posted code, consider the following code snippet:

START-OF-SELECTION.

CREATE OBJECT O_SUPER.

CREATE OBJECT O_SUB.

CALL METHOD O_SUPER->M1.

CALL METHOD O_SUPER->M2.

CLEAR O_SUPER.

(After the above clear statement, OSUPER will not point to any object memory, thus the reference to the object of the base class is lost and hence the methods of the base class are rendered inaccessible)_

O_SUPER = O_SUB.

(Now the superclass object points to the object of the subclass and now subclass methods are accessible, narrow casting is done here)

CALL METHOD O_SUPER->('M3').

SKIP 5.

ULINE 1(80).

CLEAR O_SUB.

(In the above line of code, you are deleting the reference to the object of the subclass/derived class in the memory, however OSUPER still points to the object of the subclass/derived class within the program at this point)_

O_SUB ?= O_SUPER.

(In the above line, OSUPER only has the reference to the subclass, after the narrowcasting was done a couple of lines before, thus there is no access to the base class methods at this point, only the derived class methods are accessible, thus derived class methods are called instead of the base class methods by O_SUB, your attempt to wide cast will not work)_

CALL METHOD O_SUB->M1.

CALL METHOD O_SUB->M2.

CALL METHOD O_SUB->M3.

Hope I was able to clear your doubt,

Regards,

Adithya

Edited by: Adithya K Ramesh on Dec 20, 2011 5:33 PM

Edited by: Adithya K Ramesh on Dec 20, 2011 5:36 PM

7 REPLIES 7
Read only

Former Member
0 Likes
983

Hi Srinivas,

I figured out what your problem was.

From your posted code, consider the following code snippet:

START-OF-SELECTION.

CREATE OBJECT O_SUPER.

CREATE OBJECT O_SUB.

CALL METHOD O_SUPER->M1.

CALL METHOD O_SUPER->M2.

CLEAR O_SUPER.

(After the above clear statement, OSUPER will not point to any object memory, thus the reference to the object of the base class is lost and hence the methods of the base class are rendered inaccessible)_

O_SUPER = O_SUB.

(Now the superclass object points to the object of the subclass and now subclass methods are accessible, narrow casting is done here)

CALL METHOD O_SUPER->('M3').

SKIP 5.

ULINE 1(80).

CLEAR O_SUB.

(In the above line of code, you are deleting the reference to the object of the subclass/derived class in the memory, however OSUPER still points to the object of the subclass/derived class within the program at this point)_

O_SUB ?= O_SUPER.

(In the above line, OSUPER only has the reference to the subclass, after the narrowcasting was done a couple of lines before, thus there is no access to the base class methods at this point, only the derived class methods are accessible, thus derived class methods are called instead of the base class methods by O_SUB, your attempt to wide cast will not work)_

CALL METHOD O_SUB->M1.

CALL METHOD O_SUB->M2.

CALL METHOD O_SUB->M3.

Hope I was able to clear your doubt,

Regards,

Adithya

Edited by: Adithya K Ramesh on Dec 20, 2011 5:33 PM

Edited by: Adithya K Ramesh on Dec 20, 2011 5:36 PM

Read only

sreenivas_pachva
Participant
0 Likes
982

Thanks Adithya for your immediate reply..

still i didnt get clear idea about wide casting and when does it applicable ?

please exaplain with example code with clearly.

whether object of subclass and wide casting object both are same are not? if both are not same when are we going to apply the wide casting?exaplain clearly...

Thanks&Regards

Sreenivas Pachva

Read only

0 Likes
982

Hi,

consider the following sample code:

REPORT ZA_TEST93.

class class_super definition.

public section.

data: var_area type i.

methods:

area importing length type i breadth type i.

endclass.

class class_super implementation.

method area.

var_area = length * breadth.

write:/ 'Area of rectangle = '.

write: var_area.

endmethod.

endclass.

class class_sub definition inheriting from class_super.

public section.

data:height type i.

methods:

area redefinition.

endclass.

class class_sub implementation.

method area.

var_area = 6 * length * breadth * height.

write:/ 'Area of the cube ='.

write: var_area.

endmethod.

endclass.

start-of-selection.

data: object_superclass type ref to class_super,

object_subclass type ref to class_sub,

object2_superclass type ref to class_super.

create object object_superclass.

create object object2_superclass.

create object object_subclass.

call method object_superclass->area exporting length = 10 breadth = 5.

"Narrow casting

object_subclass->height = 10.

object_superclass = object_subclass.

call method object_superclass->area exporting length = 10 breadth = 10.

"Wide casting

object_superclass ?= object2_superclass.

call method object_superclass->area exporting length = 10 breadth = 5.

_____________________________________________________________________________________________________

Explanation:

In the above code, consider the super class named 'class_super'.

This class has a method called 'area' which is used to calculate the area of a rectangle.

Consider the subclass 'class_sub', which inherits all the attributes and methods of super class 'class_super'.

Now we want to use the same method of super class 'class_super', 'area', but this time we want it to calculate the area of a cube.

For this purpose we use the 'redefinition' keyword in the definition part of 'class_sub' and enter the code for cube area calculation in the 'area' method body in the implementation part of 'class_sub'.

From the above code, consider the following code snippet:

create object object_superclass.

create object object2_superclass.

create object object_subclass.

call method object_superclass->area exporting length = 10 breadth = 5.

Explanation: Two objects of the superclass and one object of the subclass are created and the 'area' method of the superclass is called to compute the area of a rectangle.

Now consider what comes next:

"Narrow casting

object_subclass->height = 10.

object_superclass = object_subclass.

call method object_superclass->area exporting length = 10 breadth = 10.

Explanation:

We assign a value of 10 to the 'height' instance variable of the subclass object.

Then we perform narrow casting in the next line(object_superclass = object_subclass.).

Now the instance of the superclass(object_superclass) now points or refers to the object of the subclass, thus the modified method 'area' of the subclass is now accessible.

Then we call this method 'area' of the subclass to compute the area of the cube.

Moving on to the final piece of code:

"Wide casting

object_superclass ?= object2_superclass.

call method object_superclass->area exporting length = 10 breadth = 5.

Explanation:

The object 'object_superclass' now refers to the object of the subclass 'object_subclass'.

Thus the 'area' method of the superclass cannot be called by this object.

In order to enable it to call the 'area' method of the superclass, wide casting has to be perfomed.

For this purpose, the RHS of the wide casting must always be an object of a superclass, and the LHS of the wide casting must always be an object reference declared as 'type ref to' the superclass(objectsuperclass ?= object2_superclass.)._

Otherwise a runtime error will occur.

Finally the 'area' method of the superclass can now be called once again to compute the area of the rectangle.

The output of the above example program is as follows:

Area of rectangle = 50

Area of the cube = 6,000

Area of rectangle = 50

Also note that wide casting cannot be performed on subclass objects, wide casting can only be performed on superclass objects that have been narrow cast.

Hope my explanation was useful,

Regards,

Adithya.

Edited by: Adithya K Ramesh on Dec 21, 2011 11:49 AM

Edited by: Adithya K Ramesh on Dec 21, 2011 11:51 AM

Read only

sreenivas_pachva
Participant
0 Likes
982

Thanks for your reply.

now i got it what is wide casting but in which scenarios are we going for the wide casting?

Thanks&Regards

Sreenivas Pachva

Read only

0 Likes
982

Actually when we create the instance of sub class then we get access to the functionalities of super & sub class. Other than this we use interfaces & abstract classes polymorphically, then the object references are decided only at runtime and it wont be known which class is to accessed until the runtime. In such cases we go for casting.

For examples you can refer the RTTS classes available.

Please search for the available information before posting. There are lot of examples & blogs of when to & how to use casting.

Read only

0 Likes
982

Hi,

narrow casting is used in tandem with wide casting in scenarios where a desired result is needed, but the data and formula used to calculate the result varies depending upon the object.

In my example code, the required result is the area of the object, but depending on the kind of object, either a rectangle or a cube, the appropriate formula can be applied, using a single instance within the code for both cases, but calling different 'area' methods, using narrow casting and wide casting.

A better example with illustration of this is available in a downloadable pdf on the internet titled:

BC 404 ABAP Objects: Object-Oriented Programming in R/3

Thanks and regards,

Adithya.

Read only

sreenivas_pachva
Participant
0 Likes
982

Thanks to all my problem was solved.

Thanks&Regards

Sreenivas Pachva