<< Chapter < Page Chapter >> Page >

Learn through experimentation

I placed a print statement in each of the overloaded methods to display the type of that method's argument on the screen when the method is called. Byexamining the output, we can see that the method with the float parameter was called first (corresponding to the parameter of type int ). Then the method with the double parameter was called (corresponding to the parameter of type double ).

Converted int to float

Thus, the system selected the overloaded method requiring an incoming parameter of type float when the method was called passing an int as a parameter. The value of type int was automatically converted to type float .

In this case, it wasn't too important which method was called to process the parameter of type int , because the two methods do essentially the same thing -- compute and return the square of the incoming value.

However, if the behavior of the two methods were different from one another, it could make a lot of difference, which one gets called on an assignmentcompatible basis. (Even in this case, it makes some difference. As we will see later, when a very large int value is converted to a float , there is some loss in accuracy. However, when the same very large int value is converted to a double , there is no loss in accuracy.)

Avoiding the problem

One way to avoid this kind of subtle issue is to avoid passing assignment-compatible values to overloaded methods.

Passing assignment-compatible values to overloaded methods allows the system to resolve the issue through automatic type conversion. Automatic typeconversion doesn't always provide the best choice.

Using a cast to force your choice of method

Usually, you can cast the parameter values to a specific type before calling the method and force the system to select your overloaded method of choice.

For example, in this problem, you could force the method with the double parameter to handle the parameter of type int by using the following cast when the method named square is called:

square((double)x)

However, as we will see later, casting may not be the solution in every case.

Back to Question 2

Answer 1

C. 9 17.64

Explanation 1

What is method overloading?

A rigorous definition of method overloading is very involved and won't be presented here. However, from a practical viewpoint, a method is overloaded whentwo or more methods having the same name and different formal argument lists are defined in the class from which an object is instantiated, or are inherited intoan object by way of superclasses of that class.

How does the compiler select among overloaded methods?

The exact manner in which the system determines which method to call in each particular case is also very involved. Basically, the system determineswhich of the overloaded methods to execute by matching the types of parameters passed to the method to the types of arguments defined in the formal argumentlist.

Assignment compatible matching

However, there are a number of subtle issues that arise, particularly when there isn't an exact match. In selecting the version of the method to call,Java supports the concept of an "assignment compatible" match (or possibly more than one assignment compatible match) .

Briefly, assignment compatibility means that it would be allowable to assign a value of the type that is passed as a parameter to a variable whose typematches the specified argument in the formal argument list.

Selecting the best match

According to Java Language Reference by Mark Grand,

"If more than one method is compatible with the given arguments, the method that most closely matches thegiven parameters is selected. If the compiler cannot select one of the methods as a better match than the others, the method selection process fails and thecompiler issues an error message."

Understanding subtleties

If you plan to be a Java programmer, you must have some understanding of the subtle issues involving overloaded methods, and therelationship between overloaded methods and overridden methods. Therefore, the programs in this module will provide some of that information and discuss someof the subtle issues that arise.

Even if you don't care about the subtle issues regarding method overloading, many of those issues really involve automatic typeconversion. You should study these questions to learn about the problems associated with automatic type conversion.

This program is straightforward

However, there isn't anything subtle about the program for Question 1 . This program defines two overloaded methods named square . One requires a single incoming parameter of type int . The other requires a single incoming parameter of type double . Each method calculates and returns the square of the incoming parameter.

The program calls a method named square twice in succession, and displays the values returned by those two invocations. In the first case, an int value is passed as a parameter. This causes the method with the formal argument list oftype int to be called.

In the second case, a double value is passed as a parameter. This causes the method with the formal argument list of type double to be called.

Overloaded methods may have different return types

Note in particular that the overloaded methods have different return types. One method returns its value as type int and the other returns its value as type double . This is reflected in the output format for the two return vales as shown below:

9 17.64

Back to Question 1

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask