Previously we covered the basics of writing classes and method in this article. Because object-oriented-programming (OOP) concepts are such an important topic in Java, we’re coming back to covering the rich OOP features offered by Java! In this article we are going to start slow and cover the simple topic of method overloading.
Method overloading is to have multiple methods having the same name but different parameter list. When we say the same parameter list we mean the same number of parameters, with the same types, in the same orders. It’s intuitive that the name of parameters don’t matter.
The following shows an example of two overloaded method named
The methods have the same name, but the first version has 2 parameters and the second version has 3.
Return type of the methods does not matter. The following matrix shows this relationship, consider that we’re overloading two methods (they have the same name).
|Same Paramethers||Same Return Type||Result|
|No||No||Compiling ok; overloaded methods|
|No||Yes||Compiling ok; overloaded methods|
|Yes||No||Compiling error: method already defined|
|Yes||Yes||Compiling error: method already defined|
There are two reason for overloading a methods. The first is to support different data types. If you look at the
println() method of
PrintStream class, which is used to output a line of text to the screen, you can see that there are different versions of the method, for different types. In this case, we can say the method is overloaded. The following shows a few overloaded version of
println() from Java documentation.
- println(int x)
- println(float x)
- println(String x)
The second reason is to make some parameters of a method optional. For example, the following methods finds the minimum value of an array, but the second version has an additional second parameter that specify where the method should start looking. The implementation are omitted because they are not important.
This pattern is very popular in writing constructors. A class may have default values, and different versions of constructors take different numbers of parameters. Depending on the version called, the parameters that are not assigned get the default value.
The following shows an example of two version of constructor. When the first version of the constructor is called, a square will be created. The user has the option of calling the second version for a non-square. The main method is shown below the
Note that the first constructor actually calls the second constructor, using
this(). However, calling another constructor has to be the first statement in a constructor.
static method be overloaded? Static methods, like
main() can be called without class instantiation. The answer is yes,
static class can be overloaded, just as the first example above.
Of course, non-static methods can be overloaded as well.
The other question is can a non-static method overload a static method? The answer is, the code will compile just fine, but it’s not really an overloading. The reason is that you can call the static version without instantiation, but you need an object for the non-static version. This is show below.