3.4. Create an overloaded method

[6.1] Create methods with arguments and return values; including overloaded methods

Overloaded methods are methods with the same name but different method parameter lists. In this section, you’ll learn how to create and use overloaded methods.

Imagine that you’re delivering a lecture and need to instruct the audience to take notes using paper, a smartphone, or a laptop—whichever is available to them for the day. One way to do this is give the audience a list of instructions as follows:

  • Take notes using paper.
  • Take notes using a smartphone.
  • Take notes using a laptop.

Another method is to instruct them to “take notes” and then provide them with the paper, a smartphone, or a laptop they’re supposed to use. Apart from the simplicity of the latter method, it also gives you the flexibility to add other media on which to take notes (such as one’s hand, some cloth, or the wall) without needing to remember the list of all the instructions.

This second approach, providing one set of instructions (with the same name) but a different set of input values can be compared to using overloaded methods in Java, as shown in figure 3.17.

Again, overloaded methods are methods that are defined in the same class with the same name, but with different method argument lists. As shown in figure 3.17, overloaded methods make it easier to add methods with similar functionality that work with different sets of input values.

Figure 3.17. Real-life examples of overloaded methods

Let’s work with an example from the Java API classes that we all use frequently: System.out.println(). The println method accepts multiple types of method parameters:

When you use the method println, you know that whatever you pass to it as a method argument will be printed to the console. Wouldn’t it be crazy to use methods like printlnInt, printlnBool, and printlnString for the same functionality? I think so, too. But opinions change across different conditions. At times, you might use specific methods instead of overloading because it reads well and avoids confusion. As you work with more code, you’ll be able to judge these situations for yourself.

Rules to remember for defining overloaded methods

Here are a few rules for defining overloaded methods:

  • Overloaded methods must have method parameters different from one another.
  • Overloaded methods may or may not define a different return type.
  • Overloaded methods may or may not define different access levels.
  • Overloaded methods can’t be defined by only changing their return type or access modifiers or both.

Next, I’ll describe in detail the preceding rules—valid argument list, return types, and access level to define overloaded methods.

3.4.1. Argument list

Overloaded methods accept different lists of arguments. The argument lists can differ in terms of any of the following:

  • Change in the number of parameters that are accepted
  • Change in the types of parameters that are accepted
  • Change in the positions of the parameters that are accepted (based on parameter type, not variable names)

Following is an example of the overloaded method calcAverage, which accepts different numbers of method parameters:

The preceding code is an example of the simplest flavor of overloaded methods. You can also define overloaded methods in which the difference in the argument list is in the types of the parameters that are accepted:

But you can’t define overloaded methods by just switching an array parameter into a vararg or vice versa (unless the vararg or array item type doesn’t remain the same). Behind the scenes, varargs are implemented as arrays. So the following overloaded methods won’t compile:

The methods are also correctly overloaded if they change only the positions of the parameters that are passed to them:

Although you might argue that the arguments being accepted are one and the same, with only their positions differing, the Java compiler treats them as different argument lists. The compiler can understand which method implementation you want to call by looking at the sequence of arguments you specified in your code. Hence, the preceding code is a valid example of overloaded methods.

But an issue arises when you try to execute this method using values that can be passed to both versions of the overloaded methods. In this case, the code will fail to compile:

In the preceding code, defines the method calcAverage, which accepts two method parameters: a double and an int. defines the overloaded method calcAverage, which accepts two method parameters: an int and a double. Because an int literal value can be passed to a variable of type double, literal values 2 and 3 can be passed to both of the overloaded methods declared at and . Because this method call is dubious, fails to compile.

3.4.2. Return type

Methods can’t be defined as overloaded methods if they differ only in their return types, because return type is not part of a method signature:

Methods in the preceding code can’t be termed overloaded methods.

3.4.3. Access level

Methods can’t be defined as overloaded methods if they differ only in their access levels:

If you define overloaded calcAverage methods as shown in the preceding code, the code won’t compile.

In the next section, you’ll create special methods called constructors, which are used to create objects of a class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.198