• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded methods  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been reading up on overloaded methods but I'm having some issues understanding how to go about doing so. Does anyone know of any short examples that I could look at for a reference?
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julie Nelson:
I've been reading up on overloaded methods but I'm having some issues understanding how to go about doing so. Does anyone know of any short examples that I could look at for a reference?


If I understand you correctly, you are looking for some examples of how to implement overloaded methods.

The Apache Commons Lang project has a number of class with many overloaded methods. You can download the full source code here (just click on the "2.4.zip" link under the "sources" header). You can also browse the source code online here. Just navigate to a class, then click on the "view" link next to where is says "head". You can use the JavDocs to look for some overloaded methods. I also list a few at the end of this post.

For example, look at the org.apache.commons.lang.StringUtils class (online browsing). There are three overloaded versions of the center method. In one case, the one center method simply calls another one of the center methods passing in a default value. In the other case though, since the logic is completely different, the methods are very different.

As this one example shows, how you implement overloaded methods will vary. If it is a simple case that one version uses a default value for something, where as the other version allows you to specify it, this is a common way of doing it:



We see that in the center example.

Other times an overloaded method might need to convert some data. For example, let's sat I have a method that reads something from a File. I want to provide flexibility, so I want to allow a version of the method that:
  • takes a File object
  • takes a directory as a File object and a file name as a String
  • takes a directory path as a String and a file name as a Sting
  • takes a String representing the full path to the file

  • So I could write this:


    In this example, we have one method that is the "primary" method that does all the work. The other overloaded methods simply convert some data to a common format and than call the "primary" method. Also notice in that example that all the methods can throw a FileNotFoundException, but only the "primary" version of the method creates the exception. It simply bubbles up.

    The above example illustrates what is known as the DRY principle. DRY = don't repeat yourself. I could have put the same 50 lines of code that reads from the file in all four methods. But that would be silly and error prone. If I need to make a change, I must remember to change it in all four places. By extracting it out into a common chunk of code, and having three versions of the method all call the forth, we have made the code easier to maintain, and read.

    In other cases, you might have some overloaded methods that have very different logic. This happens when there is no common code to extract. In that case you simply have two methods (named the same thing and returning the same type) with their own logic.

    Sometimes these last two cases combine. Let's say I want to have two versions of a method that take two different objects. I can't convert one to the other, but at a certain point in the processing the logic becomes the same. I might do this:



    Notice here I have two publicly exposed methods and one common private worker method. I simply would put the common work into that common worker method to keep my code DRY.

    Some other examples you could look at in the Apache Commons Lang project would be
  • The two wrap methods in WordUtils
  • The seven random methods in RandomStringUtils
  • The two isSameDay methods in DateUtils

  • I hoe that helps.
    [ December 10, 2008: Message edited by: Mark Vedder ]
     
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you go through your Java installation folder you will find a file called src.zip. Unzip that to some convenient location elsewhere on your computer and remember where. Open the folder called java, then lang, then you can find lots of classes with overloaded methods. Look at the Math class and the max min or abs methods.
     
    Julie Nelson
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you much, I really appreciate it.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!