The Artima Developer Community
Sponsored Link

Java Answers Forum
when "null" is used in an overloaded method, most specific method is called

1 reply on 1 page. Most recent reply: Jun 13, 2007 3:36 AM by Doug Gunnoe

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Kiran Kumar

Posts: 12
Nickname: k3
Registered: May, 2007

when "null" is used in an overloaded method, most specific method is called Posted: May 30, 2007 8:40 AM
Reply to this message Reply
Advertisement
Hi

Can any one help me in understanding this concept.

class A{}
class B extends A{}
 
class SomeThing{
 
void meth1( A a ){
System.out.println("Inside A");
}
void meth1( B b ){
System.out.println("Inside B");
}
 
public static void main( String []args ){
SomeThing t = new SomeThing();
t.meth1( null );
}
}
 


And this method call of "meth1( null )" in the main method calls the "meth1( B b )". Why?

I know that any method call with a null as argument calls the most specific overloaded method. But why?

Kindly provide me with an answer.

Regards
Kiran


Doug Gunnoe

Posts: 22
Nickname: dgun
Registered: May, 2007

Re: when "null" is used in an overloaded method, most specific method is called Posted: Jun 13, 2007 3:36 AM
Reply to this message Reply
I looked here http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html at 15.12.2.2, but did not find an answer I liked. You may have better luck.

Now I will take a wild guess, which will probably not help you very much, which probably does not matter anyway since you asked this question 2 weeks ago:

In evaluating the two methods and trying to determine which one is more specific, java compares the null reference to both objects "b" and "a" in the param list of the similarly named methods meth1, which could apply to either. However, since "b" (as a subclass) is also an "a" (and not the other way around) the method that takes the "b" wins.

Flat View: This topic has 1 reply on 1 page
Topic: when "null" is used in an overloaded method, most specific method is called Previous Topic   Next Topic Topic: Copying file from Server to local

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use