• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Compilation Error in overloading.

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple cast will solve the problem e.g.

For more information on overloading in java Click here
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks david .
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search in this forum for "most specific" for more information.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic