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

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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