Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the following question in one of the threads here:
import java.io.*;
class Super {
int methodOne( int a, long b ) throws IOException {
return 1;
}
float methodTwo( char a, int b )
{
return 1.0f;
}
}
public class Sub extends Super {
//Place Method here
}
Which of the following are legal method declarations to add to the class Sub? Assume that each method is
the only one being added.
a) public static void main( String args[] ){}
b) float methodTwo() { return 1.0f; }
c) long methodOne( int c, long d ) { return 1L; }
d) int methodOne( int c, long d ) throws ArithmeticException { return 1; }
e) int methodOne( int c, long d ) throws FileNotFoundException { return 1;}

My question is why would B) qualify as correct, because it doesnt take the same argument types?
Thanks.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a. is legal because we just add another method.
b. is legal attempt to overloading (name is the same and parameters are different)
c. is an illegal attempt to overriding (see the return type is different)
d. is legal because the overriding is correct
e. is legal because the overriding is correct
 
Paul Ze
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isn't answer d illegal too because it is throwing a different (in this case more exceptions) exception than the overridden one. I thought an overridding method can't throw more exceptions than the method it overrides but it can throw less.
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Valentin that answer d is legal. I tried to compile the code using option d and there was no compiler error. A method that overrides another method cannot be declared to throw more CHECKED exceptions than the method being overidden. But in this case ArithmeticException is a runtime exception and not a checked exception and so it is legal.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina,
What you mean by "CHECKED exceptions"?
Regards,
Clement
 
Mihalydeak Szilard
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hei,
Keep it simple ! As you know a set of methods are valid only if at invokation time you can select one and only one method based on the arguments you pass.
Try compiling, you'll get:
javac Sub.java
Sub.java:10: methodOne(int,long) is already defined in Sub
int methodOne( int c, long d ) throws ArithmeticException { return 1; }
^
Sub.java:11: methodOne(int,long) is already defined in Sub
int methodOne( int c, long d ) throws FileNotFoundException { return 1;}
^
Sub.java:9: methodOne(int,long) in Sub cannot override methodOne(int,long) in Super; attempting to use incompatible return type
found : long
required: int
long methodOne( int c, long d ) { return 1L; }
^
3 errors
regs. Szilard,
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Szilard,
What do you mean by "As you know a set of methods are valid only if at invokation time you can select one and only one method based on the arguments you pass."
Of course if we include all the answers (a to e) in the code then the 3 errors would definitely be displayed. It was stated in the problem that we should assume that each method is
the only one being added.
Tina
 
Mihalydeak Szilard
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina,
Adding the methods one-by-one to the code, ok, but it does not change anything, the essence of the problem is the same: two ambiguous methods, that is, from the caller's point of view the two methods' declarations are the same.
The caller at the moment of invocation knows nothing about the income, the return value or the thrown exception. If simply calling methodOne(10, 100), how would you choose the right method from:
1.
int methodOne( int a, long b ) throws IOException
and
int methodOne( int c, long d ) throws ArithmeticException
or 2.
int methodOne( int a, long b ) throws IOException
and
int methodOne( int c, long d ) throws FileNotFoundException
If I correctly remember, for ex. in C++ this problem was sorted out by the compiler giving "ambiguous methods", or smth similar.
The 3rd error is clear as well, you cannot override a method by just changing return types.
More wiser people said the same thing like this:
----------
4.4 Overload Resolution
Overloaded methods have the same name as an existing method, but differ in the number and/or the types of arguments. Overload resolution involves determining which overloaded method to invoke. The return type is not considered when resolving overloaded methods. Methods may be overloaded within the same class. The order of method declaration within a class is not significant.
Methods may be overloaded by varying both the number and the type of arguments. The compiler determines which matching method has the lowest type conversion cost. Only methods with the same name and number of arguments are considered for matching. The cost of matching a method is the maximum cost of converting any one of its arguments. There are two types of arguments to consider:, object types and base types.
The cost of converting among object types is the number of links in the class tree between the actual parameter's class and the prototype parameter's class. Only widening conversions are considered. (See Casting Between Class Types for more information on object conversion.) No conversion is necessary for argument types that match exactly, making their cost 0.
----------
Szilard,
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow wow wow :roll:
Sorry to interrupt, but we have some strangers in town, here
mszilard and Clement,
we'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1.
int methodOne( int a, long b ) throws IOException
and
int methodOne( int c, long d ) throws ArithmeticException
or 2.
int methodOne( int a, long b ) throws IOException
and
int methodOne( int c, long d ) throws FileNotFoundException

Hmmm if we add, one by one, the mentioned methods to the subclass we are overriding the methods in the superclass. Thus the point is not how to decide among several methods to invoke, but if the compiles consider the overridings approvable.
[ March 18, 2002: Message edited by: Jose Botella ]
 
Mihalydeak Szilard
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok
 
Mihalydeak Szilard
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
That's right ! The point is to decide whether the compiler considers the overridings approvable...
...what in fact for us, human beings, means that invoking a method it is not ambiguous which one of two methods would be called.
regs.
Szilard,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic