Method overloading in Java needs to be use carefully. Poorly overloaded method add not only add confusions among developers who use that but also they are error prone and leaves your program on compiler's mercy to select proper method. It's best to avoid issues related to method overloading in Java by following some Java best practices. For those who doesn’t know What is method overloading in Java, method overloading means declaring two method with same name but different method signatures. This is generally done to create method which operates on different data types e.g. System.out.println() which is overloaded to accept different types of parameters like String, double, int etc, see this Java tutorial on method overloading and static vs dynamic binding for more details. By the way all of these Java best practices which are explained in context of method overloading are equally applicable to constructor overloading in Java, because in terms of overloading method and constructors are almost same.
Java Best Practices - Method Overloading
Here are some of the common things which you can remember while overloading method or constructor in Java. These Java best practices are completely based upon experience and you may have some more to add on this list. let’s see my list of Java best practices while overloading method in Java.
1) Don't overload method which accept same number of parameter with similar types
Two overloaded method which accept same number of argument with similar types i.e. which follow same type hierarchy is most common mistake while overloading method in Java. For example, find out which version of overloaded method will be invoked in following scenario :
To surprise of some programmers method with argument type List is called both the time, instead of expected method which takes ArrayList and LinkedList, because method overloading is resolved at compile time using static binding in Java. This is also one of the reason, why its important to clearly understand difference between method overloading and overriding in Java. Here expected case is result of mistaking overloading as overriding, which work on actual object and happens at runtime. To know more about static and dynamic binding in Java , you can also see my post difference between static and dynamic binding in Java.
2) Use radically different types while overloading method in Java
It's completely legal and there is no ambiguity when two overloaded method accepts radically different types like String and Integer. Though both overloaded method will accept only one parameter, it’s still clear which method is called because both types are completely different to each other. Both programmer and compiler both know which method will be invoked for a particular call. One of the example of this kind of overloading is constructor of java.util.Scanner class which accepts File, InputStream or String as parameter, as shown below :
3) Beware of Autoboxing while overloading method in Java
Prior to introduction of Autoboxing and unboxing in Java 5, method which accept primitive type and object type were radically different and it’s clear which method will be invoked. Now with autoboxing it's really confusing. Clasical example of this kind overloading mistake is ArrayList’s remove() method, which is overloaded to accept index as well as Object. when you store Integer in ArrayList and call remove() method, It’s hard to find out which remove() method will be called, as shown in below example :
Many Java programmer expect that Integer(1) object would be removed but since remove() is overloaded, compiler choose remove(int) over remove(Object). Rules of which overloaded method gets chosen in case of autoboxing is complex and hard to remember, so Its best to avoid two overloaded method where one accept Object and other accept primitive type. If by any chance you must have to do this then make sure both of them perform identical function.
Other Java best practices articles from Javarevisited Blog