This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Can someone explain the output of this program? <CODE> public class Test { 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[]) { Test question = new Test(); question.method(null); } } </CODE>
Sivalingam Sivasuthan
Ranch Hand
Joined: Jan 12, 2001
Posts: 75
posted
0
Hi Dilshad Syed: Both object reference and String reference can be assigned with null. JVM assumes that the null is used as a reference for a String Object.Because String extends the Object, So the Overloaded method with String argument will be called(at the runtime most Specific Version). -Siva
- Siva<br /> Sun Certified Programmer for the Java™2 Platform
Dilshad Syed
Greenhorn
Joined: Mar 05, 2001
Posts: 29
posted
0
So, is it safe to assume that: In case of overloaded methods, if there is ambiguity at runtime, the JVM will ALWAYS call the overloaded method with the argument of the subclass What would happen in the above example if there was no relationship between the arguments(String and Object) and both could still accept the same parameter(null)?
uma sakthi
Greenhorn
Joined: Feb 11, 2001
Posts: 10
posted
0
Hai, In that case the complier complains as reference is ambiguous(because both methods can handle and complier cannot decide) Try using String and StringBuffer as the object types in your program. Regards uma
Narsimha Manekar
Ranch Hand
Joined: Mar 05, 2001
Posts: 35
posted
0
Dilshad, I think you are right. I have just modified your program to proove your statement. // prog 1. public class Test7 { public void method(Test9 s) { System.out.println("Test9 Version"); } public void method(Test8 I) { System.out.println("Test8 Version"); } public static void main(String args[]) { Test7 question = new Test7(); question.method(null); } } class Test8{} class Test9 extends Test8{} Above program's output is: Test9 Version //progr 2 public class Test7 { public void method(Test10 s) { System.out.println("Test10 Version"); } public void method(Test9 s) { System.out.println("Test9 Version"); } public void method(Test8 I) { System.out.println("Test8 Version"); } public static void main(String args[]) { Test7 question = new Test7(); question.method(null); } } class Test8{} class Test9 extends Test8{} class Test10 extends Test8{} Above program gives compilation error, saying: Reference to method is ambiguous. ---Narsimha
Above
Originally posted by Dilshad Syed: So, is it safe to assume that: In case of overloaded methods, if there is ambiguity at runtime, the JVM will ALWAYS call the overloaded method with the argument of the subclass What would happen in the above example if there was no relationship between the arguments(String and Object) and both could still accept the same parameter(null)?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.