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

Widening Casting / Down Casting

Former Member
0 Likes
1,069

Hi Folks,

     I am learning ABAP OO and not able to catch the concept of widening casting. I also followed the below links, but still not able to understand. Kindly help.

is it mandatory to use interfaces when going for the widening casting(as I saw a couple of examples of widenin casting and everywhere they used the interface )

3 REPLIES 3
Read only

former_member188458
Active Participant
0 Likes
837

Hi Santosh ,

Wide Casting : When you move the child class object reference to parent class object reference

Narrow/Down Casting : Opposite i.e. parent class object ref to child class obj ref

e.g.  Lets Say

Parent class : Car  , Object ref : OBJ_CAR

Child Class : Sedan ( inherits Parent class Car) , object ref : OBJ_SEDAN

Wide casting :

obj_car = obj_sedan

Narrow/Down Casting :

obj_sedan ?= obj_car

Advantage of narrow casting is you can use the methods of the subclass and also that of parent class.

Hope it helps you to understand .

Best Regards,

Rini

P.S : Pls mark the answer as correct/helpful if so.

Read only

0 Likes
837

Hi Rini,

    Thanks for the quick reply. I understood the concept theoretically. but the problem I am having is implementing it practically in a program. I am also slightly confused in understanding if Interfaces are mandatory to have the widening casting implemented.

One more thing, I read somewhere that the casting operator "?=" is only used for widening casting ( Assigning the instance of a superclass to the sub-class ). However in the above explanation of yours I can see you have used the casting operator under the header "Narrow Casting" .

Regards,

  Santosh

Read only

0 Likes
837

Hi Santosh,

I just realized I mixed up the terminology ,my bad ( probably late night effect 🙂 ) .

It is the vice-a-versa . Here is the corrected one

Wide Casting : When you move the child class object reference to parent class object reference

Narrow/Down Casting : Opposite i.e. child class object ref to parent class obj ref

e.g.  Lets Say

Parent class : Car  , Object ref : OBJ_CAR

Methods : get_mileage

Child Class : Sedan ( inherits Parent class Car) , object ref : OBJ_SEDAN

Methods : get_mileage (inherited from parent but redefined)

                   get_back_space ( new method of child class)

Wide casting :

obj_sedan ?= obj_car

So when u do wide casting you can access parent class methods as well as methods belonging to child class e.g. in above case

you can access

OBJ_SEDAN->get_back_space

Narrow/Down Casting :

obj_car = obj_sedan

When you do Narrow casting  you can only access the methods belonging to parent class however if the method is re-defined in the child class , the parent class object reference calls the child class method implementation .

e.g. in above scenrio u cannot call get_back_space method using the obj_car ref , but u can call get_mileage method and because it is re-defined in the child class so it will call the child class implementation of method get_mileage .

OBJ_SEDAN->get_mileage


Regarding your question related to Interface : Implementing Interface in classes is another kind of polymorphism(providing multiple forms)  , which you can think as simlar to inheritance i.e.  parent class & child class relation .

class . Only difference is Interface methods have no implementation but the implementing class has the implementation.


e.g. if we take the above case again , lets say


Interface : IF_CAR

methods : get_mileage


Class which Implements IF_IAR : CL_SEDAN

methods : get_milage ( with method code / implementation)

                  get_back_space ( new method of child class)


and you have the same narrow and wide casting beahivor as explained above .


Hope it helps you to understand better.


Thanks,

Rini