From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Overloading methods with different signatures

Overloading methods with different signatures - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Overloading methods with different signatures

- 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…

Contents