Application Development 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: 

Method Overloading in ABAP

Former Member
0 Kudos
764

Hi all,

I wanted to know if there is specific any reason behind not supporting method overloading by ABAP objects. I was wondering when method overloading is one of the interesting features of OO, why are we not utilizing it straight as its made use of in Java.

Just like the following alogorithm:

Class C{

int add(int a, int b) {

return a+b;

}

String add(String s1, String s2) {

return s1.concat(s2);

}

}

Now i can comfortably call add on int or string.

C c1 = new C();

c1.add(1,2);

c1.add('"a", "b")

As I am new to ABAP I am not sure if there is an alternative way to achieve the same.

thank you,

Kavitha.

1 ACCEPTED SOLUTION

0 Kudos
200

Hi,

YES there is an alternative way to achive this that is using the OPTIONAL parameters.

Since we have the concept of OPTIONAL paramters it is not required for us to support the method overloding mechanisam. Just group all the paramters that you want to support and then make some of them optional then provide implementation for those fields in such a way that only if they are supplied the code is executed.

Regards,

Sesh

8 REPLIES 8

0 Kudos
201

Hi,

YES there is an alternative way to achive this that is using the OPTIONAL parameters.

Since we have the concept of OPTIONAL paramters it is not required for us to support the method overloding mechanisam. Just group all the paramters that you want to support and then make some of them optional then provide implementation for those fields in such a way that only if they are supplied the code is executed.

Regards,

Sesh

uwe_schieferstein
Active Contributor
0 Kudos
200

Hello Kavitha

There is a simple reason why we cannot have overloading of methods in ABAP-OO: The methods of a class/interface are stored within a normal DB table where they are identified by their unique key (composed of class/interface name and method name).

Regards

Uwe

0 Kudos
200

Hello Uwe,

for e.g. local classes this restriction does not hold true. The Restriction on the meta data of the class builder are more an effect than a cause.

Best Regards

Klaus

0 Kudos
200

Hi Uwe,

Thank you for the valuable info on this. Just to not disturb the way how methods are stored, we dont have the method overloading is little unclear to me. I feel its one of the nicest feature of OO and we are just letting it un utilized.

regards,

Kavitha.

0 Kudos
200

Hi,

Its a nice feature but when we have the alternative like OPTIONAL paramters it does not make much sense to have this feature too as we can achevie the thing with OPTIONAL paramters.

Regards,

Sesh

0 Kudos
200

Hello Klaus

You are (again) perfectly right. Methods are stored in table <b>TMDIR </b>where the unique key is composed of class/interface name and index whereas the method name is just an attribute. Thus, the DB table would allow to store overloaded methods (having different indices).

Regards

Uwe

0 Kudos
200

Sesh,

If i am not wrong, what happens in optional parameters is that, we have one or more optional parameters, and in the method implementation we do an if else or a when check and have more than one method flow and achieve that. But normally an OO language is capable of matching which version of the method was called instead of you manually going and running a if else check or a when check on the optional parameter and then decide what flow you want, we could be erroneous in trying to handle all the cases.

regards,

Kavitha

Message was edited by:

Kavitha S P

former_member183804
Active Contributor
0 Kudos
200

Hello Kavitha,

While both ABAP and Java are OO languages they also have there differences. Java supports overloading of methods and identifies parameters by position. ABAP has no overloading of methods, identifies parameters by name and has optional formal parameters. With optional parameters you may simulate overloading to some extend, but honestly spoken stressing this too much results in unclear signatures. Using similar names is an alternative too.

Another interesting think are interfaces (and their methods). In Java some magic matching takes place; in ABAP interfaces form a name space of their own. So there are possibly multiple IF_SOMETHING~GET_NAME methods.

Best Regards

Klaus