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

downcasting

Former Member
0 Likes
432

Hi Experts,

http://scn.sap.com/thread/3310329

I read the above posting to understand especially downcasting (widening casting ?=).

How I need to understand it generally?

subclass ?= superclass.

With widening casting (Downcasting)  =    accesing/calling  the methods of subclass to runtime.

Which method is called? Method of subclass or superclass?

If so, with the refrence "subclass" it is anyway possible to access to its methods. Why this casting??

With upcasting (Narrow Casting ) =   accesing/calling  the methods of superclass

superclass = subclass.

With the refrence "superclass" I am calling the methods of superclass.

Is that right?


Which method is called? Method of superclass or subclass ?


Please correct me if I have misunderstood, it is very important to clarify me on this topic. I would be very grateful to everybody.

Regards

Alex

2 REPLIES 2
Read only

wol
Active Participant
0 Likes
399

Hello Alexander,

casting does NOT influence the Implementation of the object, thus it does not influence which code is called if a specific method is called.

If the concrete object is of type x, the implementation of type x is always called, whatever you have casted.

Casting only changes the glasses through which you "see" the object. Not more and not less.

Regards

Stefan

Read only

wol
Active Participant
0 Likes
399

Hello Alex,

first of all, I strongly recommend to avoid downcasting (widening) if ever possible.

Could you show us an example of your concrete problem?

As said, casting does NOT influence the called implementation:

The only moment where is fixed which implementation will be called is the creation type of the object.

This type which is created defines the method implementation (and all other properties).

An Example:


CLASS lcl_hans DEFINITION.

   PUBLIC SECTION.

     METHODS: call_me RETURNING value(re_my_name) TYPE string
            .

ENDCLASS.                    "lcl_hans DEFINITION

CLASS lcl_hans IMPLEMENTATION.

   METHOD call_me.

     re_my_name = 'Hans'.

   ENDMETHOD.                    "call_me

ENDCLASS.                    "lcl_hans IMPLEMENTATION

CLASS lcl_son_of_hans DEFINITION INHERITING FROM lcl_hans.

   PUBLIC SECTION.

     METHODS: call_me REDEFINITION
            .

ENDCLASS.                    "lcl_son_of_hans DEFINITION

CLASS lcl_son_of_hans IMPLEMENTATION.

   METHOD call_me.

     re_my_name = 'Son of Hans'.

   ENDMETHOD.                    "call_me

ENDCLASS.                    "lcl_son_of_hans IMPLEMENTATION


CLASS lcl_test_hans DEFINITION FOR TESTING.

   PUBLIC SECTION.
     METHODS: ask_hans FOR TESTING
            , ask_son_of_hans FOR TESTING
            , ask_upcasted FOR TESTING
            , ask_downcasted FOR TESTING
            .

ENDCLASS.                    "lcl_test_hans DEFINITION

CLASS lcl_test_hans IMPLEMENTATION.

   METHOD ask_hans.

     DATA: l_my_hans           TYPE REF TO lcl_hans
         , l_name_of_my_hans   TYPE string
         .

     CREATE OBJECT l_my_hans.

     l_name_of_my_hans = l_my_hans->call_me( ).

     WRITE: / 'Result of asking hans:', l_name_of_my_hans.

   ENDMETHOD.                    "ask_hans

   METHOD ask_son_of_hans.

     DATA: l_my_son_of_hans           TYPE REF TO lcl_son_of_hans
         , l_name_of_my_son_of_hans   TYPE string
         .

     CREATE OBJECT l_my_son_of_hans.

     l_name_of_my_son_of_hans = l_my_son_of_hans->call_me( ).

     WRITE: / 'Result of asking son_of_hans:', l_name_of_my_son_of_hans.

   ENDMETHOD.                    "ask_son_of_hans

   METHOD ask_upcasted.

     DATA: l_my_hans           TYPE REF TO lcl_hans
         , l_my_son_of_hans    TYPE REF TO lcl_son_of_hans
         , l_name_of_my_hans   TYPE string
         .

     CREATE OBJECT l_my_son_of_hans.

     l_my_hans = l_my_son_of_hans.


     l_name_of_my_hans = l_my_hans->call_me( ).

     WRITE: / 'Result of asking upcasted:', l_name_of_my_hans.

   ENDMETHOD.                    "ask_casted

   METHOD ask_downcasted.

     DATA: l_my_hans                      TYPE REF TO lcl_hans
         , l_my_son_of_hans               TYPE REF TO lcl_son_of_hans
         , l_my_downcasted_son_of_hans    TYPE REF TO lcl_son_of_hans
         , l_name_of_downcstd_son_of_hans TYPE string
         .

     CREATE OBJECT l_my_son_of_hans.

     l_my_hans = l_my_son_of_hans.

     l_my_downcasted_son_of_hans ?= l_my_hans.


     l_name_of_downcstd_son_of_hans = l_my_downcasted_son_of_hans->call_me( ).

     WRITE: / 'Result of asking downcasted:', l_name_of_downcstd_son_of_hans.

   ENDMETHOD.                    "ask_downcasted

ENDCLASS.                    "lcl_test_hans IMPLEMENTATION

Output:

Regards

Stefan