<< Chapter < Page | Chapter >> Page > |
C. 9 17.64
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.
A. Compiler Error
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){
C. float 9.0 double 17.64
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?
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?