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

Narrow casting vs wide casting

Former Member
0 Likes
2,234

Hello All,

I have small requriemn..

I have a super class A and a sub class B.

In both the classes I have a method XYZ.

So, A has the following methods

ABC

XYZ

B has the following methods

XYZ

Now I have want to access the sub class methods from super class...i.e., in my example...Method XYZ of B class needs to be accessed from method ABC or XYZ or A.

How do I do?

And more over..what is the difference between Narrow Casting and Wide casting?

Kalyan

3 REPLIES 3
Read only

Former Member
0 Likes
872

Hi Priya,

Consider the below Example for accessing the subclass method from Superclass:

CLASS LCL_SUPERCLASS DEFINITION.

PUBLIC SECTION.

METHODS: ABC, XYZ.

ENDCLASS.

CLASS LCL_SUPERCLASS IMPLEMENTATION.

METHOD ABC.

CALL METHOD ME->XYZ.

ENDMETHOD.

METHOD XYZ.

ENDMETHOD.

ENDCLASS.

CLASS LCL_SUBCLASS DEFINITION

INHERITING FROM LCL_SUPERCLASS.

PUBLIC SECTION.

METHODS XYZ REDEFINITION.

ENDCLASS.

CLASS LCL_SUBCLASS IMPLEMENTATION.

METHOD XYZ.

………

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: O_SUPER TYPE REF TO LCL_SUPERCLASS,

O_SUB TYPE REF TO LCL_SUBCLASS.

CREATE OBJECT: O_SUPER, O_SUB.

CALL METHOD O_SUPER->ABC.

CALL METHOD O_SUB->ABC.

In the above example, When you call the superclass method ABC using CALL METHOD O_SUPER->ABC, as you see in the implementation part of the Method it will inturn call the Superclass method XYZ.

Here ME will point to the instance of the Superclass.

But the CALL METHOD O_SUB->ABC will call the method ABC ( this is inherited from Superclass to Subclass )and inturn it calls the method XYZ of the SUbclass since ME now points to the object O_SUB of the Subclass.

Hope this is clear.

Regards,

Nirupamaa.

Read only

Former Member
0 Likes
872

Hi,

chk out this link:

Regards,

Anjali

Read only

Former Member
0 Likes
872

narrow cast is whn u wnt 2 access d redifined methods of parent class in child class or in other wrds whn u assign the instance of sub class 2 d reference of super class..

widening cast is whn u wnt 2 access d new methods of child class or in other wrds whn u assign the reference of super class to child class.