• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null passed as parameter

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test1{
static void method(String str){
System.out.println("String");
}
static void method(Object obj){
System.out.println("Object");
}
public static void main(String args[]){
method(null);
}
}

o/p is: String

when method(null) is called ...then why method(String) is invoked and why not method(Object)...the fundamentals are Object and String can both accept null values....unable to understand...help appreciated..

thanking you
amal shah
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because String is most specific then object.
Check the code below.

i am passing Child object to method now all 3 methods can take Child object
so all three methods match.But 3rd method is choosen by compiler because it is more specific then other 2 methods.
if you will remove 3rd method from above code then 2nd method will match and out put will be
SubClass Msg
lly if you will remove 2nd and 3rd method from above code then only method will remain is first method and compile will make match with 1st method
which has only choice left for comiler.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I always think it's better to figure these problems out yourself rather than being explicitly told, and so at the risk of answering a question with a question, have a look at this code:



Compile and run it and see what output you get. Now experiment by commenting out the "method" methods above, one at a time and observe what happens to the output.

Can you see what's going on with the hierarchy of classes? Does this answer your question?

Best wishes,

Daniel
 
Daniel Bryant
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like I was beaten to it!

Daniel
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Selecting the Most Specific Method
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks gowher,daniel and wise...that helped me to get my feet in water..now tryin to swim...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is also addressed in our SCJP FAQ.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic