<< 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-

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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