• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mock questions

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are two questions from one of mock exams
1).
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
What will be result if you try to compile and run this code.
1. Doesn't compile
2. The code compiles cleanly and shows "Object Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime

2).
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
What will be the output when you try to compile and run this code?
1. Doesn't compile
2. The code compiles cleanly and shows "StringBuffer Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime
I thought the correct answer for both of these would be 1. means It shouldn't compile because of ambiguity.
But correct answers are 3 and 1 respectively for question 1 & 2.
can anyone explain this to me?
Thanks in advance
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Niraj,
This is a real good question. After looking at the error displayed when trying to compile the code in question 2, you can see that null is considered to be a String/StringBuffer and not as an Object.
Hence for question 1 it displays "String version".
In question 2 there are two methods (one accepting type String and the other accepting type StringBuffer) and since null is accepted as both these types, there is an ambiguity and the code does not compile.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I had the same doubt. it got cleared
 
Niraj Sheth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudhir
Thank you for your reply. But my doubt still remains. Actually your answer is my question. There are two overloaded functions. One is expecting ref. to Object and the other is expecting ref. to String( or StringBuffer ). When we call function with null argument i.e. aMethod( null ); . Since any ref. to any Object can be null and String is also an Object, why function with String argument is being called ?
Thanks
Niraj
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the explanation of this problem has to do with how Java determines the method signature. One step Java does, is to search the most specific method. The Java language specification defines a method to me most specific as follows:
"The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method
could be passed on to the other one without a compile-time type error."
this means for your problem:
1) cause String is derived from Object, you can first call the String-version and you could call the Object-version without a compile-time error.
Same would be with Exception and IOException, this would work too...
2) cause StringBuffer and String have nothing in common :-)), that means neither of them is derived from the other, you can't use one and call the other without an compile-time error. That's the reason why they are ambigious....
Hope that the explanation is correct and explains the questions...
cheers
Oliver
 
Niraj Sheth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Oliver,
I think as you said, String is more specific than Object. Java always tries to find more specific match. So it matches null with String rather than Object. Now I feel comfortable with it.
Niraj
 
reply
    Bookmark Topic Watch Topic
  • New Topic