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
Sudhir Bangera
Ranch Hand
Joined: Oct 10, 2000
Posts: 50
posted
0
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.
madhumathi k
Greenhorn
Joined: Nov 01, 2000
Posts: 5
posted
0
Thanks I had the same doubt. it got cleared
Niraj Sheth
Greenhorn
Joined: Oct 26, 2000
Posts: 16
posted
0
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
Oliver Grass
Ranch Hand
Joined: Nov 02, 2000
Posts: 65
posted
0
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
Joined: Oct 26, 2000
Posts: 16
posted
0
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