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 - Java Tutorial
From the course: Java 8+ Essential Training: Syntax and Structure
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Evaluate conditions with if-else4m 17s
-
Evaluate conditions with switch-case4m 32s
-
(Locked)
Create looping code blocks7m 13s
-
(Locked)
Create reusable code with methods8m 17s
-
(Locked)
Create overloaded methods3m 27s
-
(Locked)
Pass arguments by reference vs. value4m
-
(Locked)
Challenge: A more complex calculator1m 18s
-
(Locked)
Solution: A more complex calculator2m 20s
-
(Locked)
-