Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Passing null references

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is anyone able to tell me what the rules are around passing null references to overloaded methods? I have look around but been unable to find any explanations.

Thanks in advance

Jason Keating

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, when you have an overloaded method that receives a reference to an object, the JVM chooses the method whose signature contains the most specific object.

In your code example, the JVM chooses the second one one because String is more specific than Object.

It really works.

Try the following code :


Output is :

A call
B call
C call
C call
A call It was decided at compile time because you're explicitly passing an argument of type A, even though its reference is of type C
 
Jason Keating
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks thats a huge help
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It was decided at compile time because you're explicitly passing an argument of type A, even though its reference is of type C



I think the reference variable is of type A but it refers to an object of type C.
Please Let me know if I am wrong.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding Further to it
If there are more one method and there is more than one choice for a specific object... the compiler freaks out and wont let you compile the code

E.g.



Error MSg :
Beta.java:16: reference to m1 is ambiguous, both method m1(java.lang.String) in
Beta and method m1(java.lang.StringBuffer) in Beta match
beta.m1(null);
[ February 23, 2006: Message edited by: Yogesh Chhawasaria ]
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"If there are more than one method and there is more than one choice for a specific object... the compiler freaks out and wont let you compile the code
"

yogesh can you please explain this in more detail....
i am confused because in overloaded methods we always have more than one method (referring above example of class A,B,C) and showMessage(Object o)
and showMessage(C o) there is more than one choice....
i think i am not clear with that...please explain....

can we say that the methos is called according to the type of refrence variable?
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi swapnil,

Yogesh is correct. Im my above example, the code compiles just because all classes (A,B,C) are within the same inheritance tree. So, it's easy to compiler determine the most specific class.

However, when you have two specific classes belonging to differentes inheritance tree (as shown in Yogesh's example) the compiler complain.

Please take a look :



Concerning to Murali's affirmation, it's the same as mine.

The argument is of type A but it refers to an instace object of type C.
 
swapnil paranjape
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx a lot ...that was indeed great explanation.....really of great help.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic