GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 162 of 502    Print  
if two overloaded methods are-
test(Object xyz) and test(Customer cust)[Assume Customer is a class with member name of type String].Now if we call test(null), which method will be called? why?



  
Total Answers and Comments: 4 Last Update: January 13, 2006     Asked by: chandu_ps2003@yahoo.co.in 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by:
 
The method - test(Customer cust) will be called. This will happen because while resolving calls to overloaded methods, the Java compiler incorporates a 'Most Specific' algorithm. Since the value null is assignable to both an 'Object' reference and a 'Customer' reference, the compiler chooses the Customer reference as an execution of the general rule that argument Types which are more specialized ('Customer' in this case) to handle a parameter passed to a method will receive that parameter as opposed to a more general type ('Object' in this case) . Consider an analogous example to make it easy to understand what I mean :Class OverloadingTest{ private void overloadedMethod(Object objectReference) { System.out.println("You passed an Object"); } private void overloadedMethod(String stringReference) { System.out.println("You passed a String"); } public static void main(String args[]) { new Test().overloadedMethod("This is a String parameter"); }}This should make it easy for you to understand what will happen and why. Our parameter - "This is a String parameter", is a String and is assignable to both an Object reference AND a String reference BUT the compiler rightly chooses the method with the String argument (i.e the second method) since it's String argument is a MORE SPECIFIC match to our String parameter than the Object argument of the first method (In the case of a null parameter it might seem meaningless but the general rule that I mentioned above, i.e a more general match is discarded in the presence of a relatively more specfic match, is followed). It certainly makes sense - what do you say ? So the second method is called and the message - "You passed a String" is printed to the console.CHEERS !!!

Above answer was rated as good by the following members:
talktoatish
November 13, 2005 17:10:36   #1  
       

RE: if two overloaded methods are-test(Object xy...
The method - test(Customer cust) will be called. This will happen because while resolving calls to overloaded methods the Java compiler incorporates a 'Most Specific' algorithm. Since the value null is assignable to both an 'Object' reference and a 'Customer' reference the compiler chooses the Customer reference as an execution of the general rule that argument Types which are more specialized ('Customer' in this case) to handle a parameter passed to a method will receive that parameter as opposed to a more general type ('Object' in this case) . Consider an analogous example to make it easy to understand what I mean :Class OverloadingTest{ private void overloadedMethod(Object objectReference) { System.out.println( You passed an Object ); } private void overloadedMethod(String stringReference) { System.out.println( You passed a String ); } public static void main(String args[]) { new Test().overloadedMethod( This is a String parameter ); }}This should make it easy for you to understand what will happen and why. Our parameter - This is a String parameter is a String and is assignable to both an Object reference AND a String reference BUT the compiler rightly chooses the method with the String argument (i.e the second method) since it's String argument is a MORE SPECIFIC match to our String parameter than the Object argument of the first method (In the case of a null parameter it might seem meaningless but the general rule that I mentioned above i.e a more general match is discarded in the presence of a relatively more specfic match is followed). It certainly makes sense - what do you say ? So the second method is called and the message - You passed a String is printed to the console.CHEERS !!!
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
November 14, 2005 07:17:39   #2  
SailajaT Member Since: November 2005   Contribution: 4    

RE: if two overloaded methods are-test(Object xy...

Thanks for the detailed explanation.


 
Is this answer useful? Yes | No
November 19, 2005 09:40:50   #3  
bhanu prasad        

RE: if two overloaded methods are-test(Object xy...
every thing is Object in the world
 
Is this answer useful? Yes | No
January 13, 2006 13:26:33   #4  
Abhishek        

RE: if two overloaded methods are-test(Object xy...
Thanks. I appreciate your time to explain in detail.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape