Become a JAVAPER – is overloading possible in ABAP?

By | January 2, 2012 | ABAP Objects, OO Concepts, Tricks | 12,984 | 4

We all know it is a lie, but I will try to prove there are huge similarities b/w both languages in this matter, which eventually can form opinion that overloading in ABAP is possible.

 

What Java says

In Java overloading is very common and useful. Within one class you can have same named methods if they have different parameter list. Below some examples of overloading:

Java overload1

 

Notice here, that what differs is parameters type or their number. The name of parameters is not significant here, nor is visibility or return type. During runtime, JVM checks for existing signatures and calls appropriate method depending on actual parameters type and order.

Java overload4

Assuming above the incorrect examples of overloading would be:

Java overload2

Here the compiler shouts that type of parameters is the same for all the methods. It doesn’t check visibility or return type. It has simple logical explanation. Which method would system call if you write it like:

Java overload3

As return value in Java is optional this factor couldn’t be decisive for overloading too. To sum up. Java allows overloading as long as parameter list and type of same named methods differs.

 

ABAP’s turn

We all know that in ABAP pure overloading is not possible. We cannot write something like:

ABAP overload1

 

But we can have definition of foo method like

ABAP overload2

This way we can call the same method in several ways.

ABAP overload3

 

Of course this is only a simple trick, which creates potential danger. In Java we don’t have to bother of which parameter is delivered to which method as all the parameters are obligatory and all the methods know exactly their signature. In ABAP we made them optional so we need a series of checks in foo method before we write our main logic.

ABAP overload4

This is not only cumbersome to write all the conditions to all the optional parameters, but also creates a risk of over sighting some of them, not mentioning that it enforces changes to foo method any time new parameter comes.

Further similarities

At first sight, the most significance difference b/w Java and ABAP approach to overloading is that each foo method on Java side can define its own return type, while ABAP can have only one.

Note!

By return type I mean here type followed by method name (in Java) and parameter preceded by RETURINIG VALUE keyword (in ABAP). In both languages they are optional during method call, although in Java you need to return anything (even it was an empty variable).

ABAP

ABAP overload5

Java

Java overload5

 

Sure, ABAP can do even more in this respect if we consider EXPORTING or CHANGING method parameters. All of them are optional so we can actually return more values that one Java counterpart method.

ABAP overload6

Note!

This is not true to CONSTRUCTORS as these are special methods with only IMPORTING parameters. CONSTRUCTORS themselves are so large and important topic in Java that they deserve separate attention. This could be a subject of next blog post.

Summary

As we can see both languages somehow support overloading. Java – officially, which is governed by its compiler and JVM; ABAP – unofficially, so we, as developers, need to ensure it is implemented correctly if we want to use it. It turns out that in ABAP we have more flexibility in this regard, as we can “return” several results. Of course to someone else using our API it may be ambiguous which parameter is real result of a function. For this purpose functional methods were designed and here ABAP gives up place to Java.

ABAP OO designers seem to take what is best in Java and put it in ABAP. You may argue that one approach is better than the other. Personally I prefer Java’s approach but this is a matter of taste. So is the choice of using overloading or not.

 

 

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Marcin Pciak{2 articles}

ABAP developer. OO programming and best practices enthusiast.
Follow :

Explore all of his 2 articles.

Load comments

4 Comments

  • karina

    Thanks for this post, very good explanation, I started to learn ABAP OO and your blog has been very usefull, regards from Mexico :)!

  • […] Become a JAVAPER – is overloading possible in ABAP? […]

  • Benny

    Good idea. Another way of doing it for simple signatures would be to use dynamic type for signatures( Type Any) and determine the type of data used in the method using RTTS.

  • Hello Benny,

    You are correct, but with using the dynamic parameter signature, it would add up processing time to map the parameters to dynamic parameters before calling the method. Within the method as well, we need to do the similar exercise.

    Regards
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.