From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

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

Create overloaded methods

Create overloaded methods - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Create overloaded methods

In Java, a class can implement more than one method of the same name, as long as each of the methods has a different signature. That means each method has to accept unique input in either the quantity or the types of its arguments. This is called overloading a method. In this example, I have a main method that's calling two different custom methods. The first custom method is called getInput, and it takes the logic for accepting input from the command line, and makes it reusable. It displays a prompt, creates a scanner object, and then returns the results of the scanner's next line method. In the main method, I'm calling that method twice, and saving the results as two different string variables. Next, I have a method called addValues, that accepts two arguments, both strings. That method has code that parses the strings, and turns them into double values, values that can be added together, and then returns the added value. Here, I'm calling the method, and then I'm displaying the…

Contents