Join Peggy Fisher for an in-depth discussion in this video Overloading functions, part of Learning Java.
- Two or more methods may have the same name as long as the list of parameters or the return type is different. When this happens it's called overloading. For example, if you wanna write a method that finds the average of three numbers you might wanna allow it to use three integers or three doubles. The logic is the same, but the type of data sent by the calling program is different. Let's take a look. Let's start by adding a method that takes three integer parameters. My method's gonna be public, static.
I need to return the average, which will be a double. The name of my method will be average and it takes three integers. Int x, int y and int c. Inside my method I wanna calculate the average and return it to the calling program. Remember, since x, y and z are integers values I need to change the answer, so I get a double. So I can do that using either implicit or explicit conversion. I'm gonna use implicit conversion.
Return. I need to first add the values x + y + z, then I can divide that by 3.0. Remember, if I divide by a double the computer will automatically choose the larger of the two, int or double, and return a double value. Okay, now let's do the same thing. Public, static, double, average. Everything is the exact same so far, but this time I'm gonna have three double values.
Double x, double y, double z. Now I can return the average. Return x + y + z divided by 3. This time I don't need to do 3.0 because x, y and z are already a double value. So my answer will be a double. Okay, last thing we need to do is go up to the main and call these two methods. So I'm just going to call them within a print line statement.
System.out.println, the average of three integers, and I'll go ahead and say 3, 5 and 8 is. Okay, now, I'm gonna call the method average and in the parentheses I need to give it those three values. Okay, right below that let's do the same thing, but this time we're gonna pass at three doubles. Now I wanna call average again and this time in the parentheses I'll give it the values that are doubles so it knows which method to call.
And I'm gonna fix this real quick and make it is:, so it's easier to understand, and 18.0. Okay, let's go ahead and run this. As you can see it printed out the average of three integers. 3, 5 and 8, which is 5.33 repeating and it also printed out the average of the three doubles, 4, 6 and 18, which is 9.33. Another example might be a method that has four parameters instead of three. Overloading methods is especially useful when we start working with classes and objects.
When we create a class to represent an instance of an object we use a constructor to build the object. This is where overloading is helpful since we might want to allow the user to create objects using default values, a partial list of values or even the entire set of values. So we can create several constructors using method overloading which allows the program to choose the right method.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs