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

Method overloading call

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


source: Enthuware
The above code prints "java.io.FileNotFoundException Version" why?
I thought it will result in an error due to ambiguity.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

FileNotFoundException is more specific than IOException which
is more specific than Object.

Whatever FileNotFoundException can be assigned can be IOException
and that can be Object to as well of course.


There is no ambiguity in this.


Thanks,
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
One doubt why the method which takes FileNotFoundException is called in the program we are not doing any file related activity.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, is that how it works. I thought, since an object can take "null" , calling the method with null argument will match all three, therefore the confusion.

I guess 'ambiguity' comes in place where there is more than one specific method at the same level. correct?

Thanks.
[ May 18, 2007: Message edited by: M Krishnan ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nik:

One doubt why the method which takes FileNotFoundException is called in the program we are not doing any file related activity.




FileNotFoundException or IOException are also classes. No matter whether
you are doing file related things or not. These classes can be used
as usually as you use other classes. There is nothing special except
they extend Exception class and can be thrown using throw keyword or
throws while declaring exception.

With normal classes this is not possible hence gives compiler error.


Thanks,
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I got the above points you guys mentioned but I didn't get the exact explanation for the same output. Could you please explain why we get the "FileNotFoundExpection" there as we didn't even try to perform any task from any file.
Please consider the below example:
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test {
public static void show(Object o) {
System.out.println("Object");
}

public static void show(IOException io) {
System.out.println("IOException");
}

public static void show(FileNotFoundException f) {
System.out.println("FileNotFoundException");
}


public static void main(String[] args) {
show(null);
}

}
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mohini!

The earlier posts tried to explain this, 14 years ago!  

We don't get any of those exceptions!

This question is really seeing if you know about the rules for method overload matching when someone passes null.

I believe all you need to know is that the compiler always picks the most specific one that matches, and ignores more general ones that happen to match.  If there are two or more choices that are equally specific, the compiler gives up and the compile fails.

Now, null is very weird, it can match anything, so as long as there is one overload that is the most specific, it will get matched.

You should slightly change the problem using other overloads with different sub-classes and convince yourself that this is really what the tricky problem was about -- nobody is throwing anything here.

A common trick on these tricky exams is for questions to appear to be about one topic, but really be testing you on another, so this one is perfectly good practice for that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic