aspose file tools
The moose likes Java in General and the fly likes Compilation Error in overloading. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Compilation Error in overloading." Watch "Compilation Error in overloading." New topic
Author

Compilation Error in overloading.

AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
Hi,
I got this error while compilation if any one of (1) or (2) were uncommented
saying that
reference to method is ambiguous both method method (java.io.IoException) in testing and
method method(java.lang.Integer) in testing match tc.method(null);

But, i am surprised how this java.io.IoException and java.lang.Integer are related .

can anybody please explain me ?


thanks in advance.

public class testing
{

public void method(java.lang.Object o)
{
System.out.println("Object Version");
}

// (1) Starts
/*public void method(java.io.IOException s)
{
System.out.println("IOException Version");
}*/
// (1) Ends

// (2) Starts
/*public void method(java.io.FileNotFoundException s)
{
System.out.println("java.io.FileNotFoundException Version");
}
*/
// (2) Ends

public void method(Integer o)
{
System.out.println("Integer Version");
}

public static void main(String args[])
{
testing tc = new testing();
tc.method(null);
}
}



AshutoshP
Amit Vinod Dali
Ranch Hand

Joined: Apr 14, 2010
Posts: 42
A simple cast will solve the problem e.g.

For more information on overloading in java Click here
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
hi, amit thanks for the reply . My doubt is i have overloaded method() with two argument types one with java.io.IOException and Integer. But, why the compiler thinks that its a ambigous method declaration resulting compilation fail

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because it's ambiguous: the compiler has no way of knowing which method should be called for a "null" parameter, because either method can take a "null" as input.
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
thanks david .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

Search in this forum for "most specific" for more information.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Compilation Error in overloading.
 
Similar Threads
Method invocation with null
why is main giving a preference to a method?
Method overloading call
question in javaquiz
mock exam question