• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Method ambiguity

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can anyone tell me why 'ss' and not 'oo' is printed after below code's execution

 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because you are sending a String as an argument (test), so the method that takes the string parameter will be the one that gets called, hence will print oo.
 
Marshal
Posts: 75708
354
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules about overloading are not easy to understand; look at the §15.12 link in that page I quoted. As an over‑simplification, if there are two alternatives, the compiler chooses the “more specific” parameter type. So String takes precedence over Object because it as a sub-class and therefore more specific.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The rules about overloading are not easy to understand; look at the §15.12 link in that page I quoted. As an over‑simplification, if there are two alternatives, the compiler chooses the “more specific” parameter type. So String takes precedence over Object because it as a sub-class and therefore more specific.



That is correct , first complier or jvm (not sure) chooses method that has more speific parameter as argument if it doesn't matches than it chooses next alternativei.e next paramenter in which argumanet can be fit
 
Campbell Ritchie
Marshal
Posts: 75708
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading: version chosen by compiler at compile time.
Overriding: version chosen by JVM at run-time.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell.

This question was asked in an interview and the interviewer said that the compiler will throw an error due to ambiguity.
 
Campbell Ritchie
Marshal
Posts: 75708
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pramod talekar wrote:Thanks Campbell.

This question was asked in an interview . . .

You’re welcome

Why didn’t you say it was an interview question? I thought it was a real-life question
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I replicated it in real time and found no ambiguity [:-p]
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pramod talekar wrote:This question was asked in an interview and the interviewer said that the compiler will throw an error due to ambiguity.


The ambiguity will come only when you call the method with a null literal.
Please note that it has to be the literal. Even passing a null String reference will enable compiler to select the right method (and hence it will not report any problem).
Lastly, sorry for being too finicky (programmer's brain's OCD), but "compiler will throw an error" does not make sense at all. Compiler can only report compilation problems. It can not throw any Error or Exception.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aditya.

Even if I pass in 'null' and run the program in Eclipse, it prints ss and not oo.
If I put in a String literal with null value, still it prints ss.

Only when I comment out the String argument method, it prints oo.
Does running the program through command line print something different ?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you invoke an overloaded method, the compiler tries to find which of these methods is "closer" (or more specific, as Campbell already mentioned) to the arguments that you are passing. So when you are passing null as argument, the compiler is thinking: "Hmmmm, he gave me a null argument and I have a method that accepts a String and an Object. String is more specific that Object, so I will invoke the method that accepts String as an argument."

For instance, try to execute the following program:




The output will be:

1)ss
2)oo
3)oo

Result 1) comes from the invocation of the aa(String s) and 3) from the aa(Object o). The compiler tried to find the most specific method related to the arguments. The same applies to result 2) where we have an Intreger as an argument. The most specific method that can be found is aa(Object o) -- remember that an Integer is not a String but it is an Object --. As an exercise, try to experiment with different methods and arguments. For example, can you find what will be printed when we execute the following program? (try to guess before you run it and see the actual results)

 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic