Quantcast
Channel: SCN : All Content - ABAP Development
Viewing all articles
Browse latest Browse all 8332

doubts in narrow casting

$
0
0

I was looking for narrow casting examples to understand concepts better.

 

 

what i have seen everywhere is that

after narrow casting that i.e assigining/copying child object to parent object we can use parent object to

call method  which exits in sub class though redefined (of course it also exists in super class).

Till here its fine.

 

 

 

But we can't use parent variable to call method in subclass which does not exist in super class.

it's showing syntax error but a little change in that have made its syntax correct.

 

Take a look at this example....

REPORT  ZNEWOO6.

*----------------------------------------------------------------------*

*       CLASS vehicle DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class vehicle definition.

   public section.

     methods: m1 ,

     m2.

 

endclass.                    "vehicle DEFINITION

*----------------------------------------------------------------------*

*       CLASS vehicle IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class vehicle implementation.

   method m1.

     write /'method m1 super class imple.'.

   endmethod.                    "m1

 

 

   method m2.

     write /'method m2 super class imple.'.

   endmethod.                    "m1

endclass.                    "vehicle IMPLEMENTATION

 

 

 

*----------------------------------------------------------------------*

*       CLASS truck DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class truck definition inheriting from vehicle.

   public section.

     methods:

     m2 redefinition,

     m4.

endclass.                    "truck DEFINITION

*----------------------------------------------------------------------*

*       CLASS truck IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class truck implementation.

   method m2 .

     write /'method m2 redefination sub class imple.'.

   endmethod.                    "m2

   method m4.

*      method m1.

     write /'method m4 sub class imple.'.

*  endmethod.

   endmethod.                    "m4

endclass.                    "truck IMPLEMENTATION

 

 

start-of-selection .

   data : o_super type ref to vehicle,

         o_sub type ref to truck.

 

   create object :o_super, o_sub.

 

   call method o_super->m1.

   call method o_sub->m1.

 

   call method o_super->m2( ).

   o_super = o_sub."up/narrow casting "assigning child to parent

*o_sub ?= o_super. "down/wide casting

   create object o_super.

 

   call method o_super->m2.

   call method o_sub->m2.

 

   "not possible to access those

*method which are not defined in super class

call method o_super->m4. "throw a syntax error

call method o_super->('m4'). "works fine WHY!!!!!! "Even though m4 is not defined in super class


Viewing all articles
Browse latest Browse all 8332

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>