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

why is main giving a preference to a method?

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

What would be the output when the above program is compiled and run?
1)It will print "Object Version"
2) It will print "java.io.IOException version"
3)It will print "java.io.FileNotFoundException Version"
4)It will not compile
5)It will throw a runtime exception
Ans: 3)
My question is that out of the 3 methods , why did the main method only choose "java.io.FileNotFoundException Version" as the output.
Any specific reasons??
Sonir
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the order of execution begin at the lower class in the tree:
Object
+-IOException
+-FileNotFoundException
byez,
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In such a situation the most specific parameter is chosen. Considering the hierarchy of the classes the java.io.FileNotFoundException is the most specific one and this is chosen.
Try replacing java.io.FileNotFoundException and u get a compiler error stating that the two methods
method(String s) and method(java.io.IOException s) match. This is because the compiler cannot identify the most specific one
/SAmith
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you guys are right. but my first instinct was to choose the method(Object o).
why, because the calling method parameter was null and null is used for objects so instinctly jumped to the method(Obejct o)
means in the exam i would have got this question as wrong !!

Originally posted by Samith Nambiar:
In such a situation the most specific parameter is chosen. Considering the hierarchy of the classes the java.io.FileNotFoundException is the most specific one and this is chosen.
Try replacing java.io.FileNotFoundException and u get a compiler error stating that the two methods
method(String s) and method(java.io.IOException s) match. This is because the compiler cannot identify the most specific one
/SAmith

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have chosen the method with the Object parameter. But this would have been my second choice after reading all the answers. My first instinct when I see null is to refer to a String.

So, the more specific the better in this case? Then why would you get the compile error between the String and the java.io.IOException methods(if you change the FNFexception to something else? Isn't java.io.IOException more specific than String?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason Kretzer:
Then why would you get the compile error between the String and the java.io.IOException methods(if you change the FNFexception to something else? Isn't java.io.IOException more specific than String?


IOException and FNFException belong to the same hierarchy. That's why the compiler can determine that FNFException is more specific. IOException and String belong to different branches and the compiler will not know which branch to choose, i.e., it becomes ambiguous.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic