Join David Gassner for an in-depth discussion in this video Overloading methods with different signatures, part of Java 8 Essential Training.
- Java supports the concept of method overloading. That means that you can create more than one version of a method. You just have to distinguish them from each other by the number of arguments or the arguments' data types, so that the Java run time knows which method to call. In this project, I've declared two methods named getInput and addValues. They both receive arguments. The getInput method receives a string named prompt. And it displays that prompt, then creates a scanner object, and uses the scanner object to return a value.
The addValues method receives two string arguments. It parses those values, adds them together, and then returns the result. Notice that both methods have data types set as the return types. GetInput returns a string, and addValues returns a double. And they're both marked as static so that they can be called from my static main method. Now, I'm going to create two more versions of the addValues method. I'll copy and paste the original version.
Notice when I paste it I get the squiggly lines on both versions of the method. That's because they have identical signatures. I have to do something to distinguish them from each other. And I'll change the new one so that it receives three string values instead of two. Then, I'll duplicate this line of code, and I'll change the names of the variables, and then I'll add the values together. And I'll return the result. Notice that the name of the new method is dimmed out.
That's a visual indicator from IntelliJ IDEA that that method isn't yet called anywhere in you code. But i'll come back to my main method, and I'll duplicate a line of code here, and I'll collect another value. And then I'll copy and paste these lines of code. I'll change the name of the result to result 2, and I'll pass in all three values. And then, I'l change the label so I can distinguish between them. So on the first call to addValues, I'll be calling my original method, and on the second one, I'll be calling the new method.
I'll make sure I'm displaying the right result, and now I'll run the code. And I'll enter three values of 2, 3, and 4. And the result for the first operation is 5, and for the second one it's 9. And again, you aren't just distinguishing by the number of arguments, you can also distinguish by the data types. I'll also show you how to create a method that receives an indeterminate number of arguments. I'll once again create a static method that returns a double value.
And I'll name it addValues just like I did before. And once again I'll receive a string value, but this time instead of naming it immediately, I'll put in three dots, an ellipse, and I'll name this values. That means that the user can pass in as many values as they want. Then, I'll work with the collection of values that's passed in. I'll use a for each loop. Within the for each loop's declaration, I'll get a string that I'll name value, and I'll get it from values.
Now before I do that loop, I'll declare a variable that I'll name result, and I'll give it a value of zero. Within the loop, I'll declare a double value named d, and I'll get its value from Double.parseDouble, and I'll pass in the value variable. That's the string. Then, I'll add that value to the existing result, and I'll use an operator called +=. That means add this to the existing value.
Finally, at the end of the loop, I'll return the result. So now this method can be called with any number of arguments. And I'll make a copy of these two lines of code, and paste them in. I'll change the name of this variable to result 3, and I'll change the value that I'm outputting, and the label. And this time I'll pass in six values. I'll take these arguments, I'll put in an extra comma, and I'll paste those in. So now, I'm adding all these values together twice.
I'll run the code, and once again I'll enter some values, 2, 3, and 4. And now I see that the answer for three values is 9, but the answer for many values is twice that, 18. So Java gives you quite a bit of flexibility in how you build your code. You can build just one version of a method, or you can build overloaded methods, distinguishing them from each other by the number and data types of their arguments.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files