<< Chapter < Page Chapter >> Page >

Back to Question 5

Answer 4

C. 9 17.64

Explanation 4

When the square method is called on an object of the Subclass type passing an int as a parameter, there is an exact match to the required parameter type of the square method defined in that class. Thus, the method is properly selected and executed.

When the square method is called on an object of the Subclass type passing a double as a parameter, the version of the square method defined in the Subclass type is not selected. The double value is not assignment compatible with the required type of the parameter (an int is narrower than a double ).

Having made that determination, the system continues searching for an overloaded method with a required parameter that is either type double or assignment compatible with double . It finds the version inherited from Superclass that requires a double parameter and calls it.

The bottom line is, overloaded methods can occur up and down the inheritance hierarchy.

Back to Question 4

Answer 3

A. Compiler Error

Explanation 3

Return type is not a differentiating feature

This is not a subtle issue. This program illustrates the important fact that the return type does not differentiate between overloaded methods having thesame name and formal argument list.

For a method to be overloaded, two or more versions of the method must have the same name and different formal arguments lists.

The return type can be the same, or it can be different (it can even be void) . It doesn't matter.

These two methods are not a valid overload

This program attempts to define two methods named square , each of which requires a single incoming parameter of type double . One of the methods casts its return value to type int and returns type int . The other method returns type double .

The JDK 1.3 compiler produced the following error:

Ap081.java:28: square(double) is already defined in Workerpublic double square(double y){

Back to Question 3

Answer 2

C. float 9.0 double 17.64

Explanation 2

This program is a little more subtle

Once again, the program defines two overloaded methods named square . However, in this case, one of the methods requires a single incoming parameterof type float and the other requires a single incoming parameter of type double . (Suffice it to say that the float type is similar to the double type, but with less precision. It is a floating type, not an integer type. The double type is a 64-bit floating type and the float type is a 32-bit floating type.)

Passing a type int as a parameter

This program does not define a method named square that requires an incoming parameter of type int . However, the program calls the square method passing a value of type int as a parameter.

What happens to the int parameter?

The first question to ask is, will this cause one of the two overloaded methods to be called, or will it cause a compiler error? The answer is that itwill cause one of the overloaded methods to be called because a value of type int is assignment compatible with both type float and type double .

Which overloaded method will be called?

Since the type int is assignment compatible with type float and also with type double , the next question is, which of the two overloaded methods will be called when a value oftype int is passed as a parameter?

Questions & Answers

the diagram of the digestive system
Assiatu Reply
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 environments
AI-Robot
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
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
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