Overloading Variable Arity Method in Java
Here we will be discussing the varargs / variable arity method and how we can overload this type of method. So let us first understand what a variable arity method is and its syntax. A variable arity method also called as varargs method, can take a number of variables of the specified type.
Note: Until version 1.4 there is no varargs method. It was introduced in version 1.5 and exists onwards.
” … ” means the method can take any number of arguments of the specified data type, even zero. Internally varargs method is implemented using a single dimension(1D) array concept.
Sum : 0 Sum : 20 Sum : 50
Now let us have a look at few important facts about the varargs method and the acceptable order of parameters. The following points will help to improve the understanding of the varargs method which are as follows:
1.1 Varargs parameters can be mixed with normal parameters.
methodName(int x, String...y)
|methodName(10 , “Hello”)||Valid|
1.2 During mixing, varargs parameter should be last
methodName(int x, String…y) valid methodName(int…x, String y) invalid
1.3 There can only be one vararg parameter
methodName(int x, int y, String…z) valid methodName(int…x, String…y) invalid
1.4 In general, varargs method will get the last priority means if no other method is matched then only varargs method will be called. The following example will sail across this concept.
varargs method Sum : 9 normal method Sum : 30
1.5 Using arrays to pass the variable number of arguments is an old approach to varargs, which is not suitable.
So let us do discuss the overloading varargs method. We know that if a class has multiple methods having the same name but different parameters, it is known as Method Overloading. The same is valid for varargs method also. Let us implement the same
Implementation: Here ‘vaTest’ is overloaded as each occurrence of vaTest expects a different argument list. Which occurrence of vaTest will be called to execute the operations on the parameters depends on the type of each parameter.
varargs method with int type arguments 1 2 3 4 5 varargs method with boolean type arguments true true false true Message 10 20
Note: A vararg method can also be overloaded by the non-varargs method. vaTest(boolean x) is valid to overload vaTest. This vaTest(boolean x) will be invoked only if one boolean argument is passed, otherwise (boolean…x).
Ambiguities: There also do occurs some ambiguities as listed:
- Call to vaTest is ambiguous because varargs can be empty and hence this call can be equally translated to either vaTest(int…x) or vaTest(int…y).
- Let us suppose we have two methods , vaTest(int…x) and vaTest(int a, int…x) . Now , if we call vaTest(1) , both formats are valid. Therefore ambiguity,
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.