• 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

overloading doubt

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain the behaviour of the following code.



Output is:
----
in object

I have read in the forum there that:
In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.

If the above rule applies here in the code, then I couldn't understand why?how? (need some spoon feeding!)
 
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
Have you seen our SCJP FAQ: What is a most-specific method? Note that the last line of that explanation addresses null references.
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi weber,

Thankyou for your reply.

But I suppose you have overlooked the code.
There is one overloaded method callMe() and a normal method testMe() (which calls callMe).

The link you refered explains why a call to 'test1.callMe(null)' fails.

But I need what actually happens if I call test1.testMe(null)
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh I got it. The link reference explains both kinds of invocations.

Just one more doubt.
Can you explain the following statement with an example.

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.

Just couldn't get it through. thankyou.
 
marc weber
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

Originally posted by Ram Manoj:
... But I need what actually happens if I call test1.testMe(null)


The null literal represents the "null type." So when you use a null literal with callMe, it fails because the argument type is ambiguous. But assigning a null literal to a variable...

Object obj = null;
test1.callMe(obj);

...will work, because the reference type of "obj" is Object, even though it doesn't point to an actual object.

This is essentially what's happening when you call testMe with a null literal. The null literal is assigned to the local variable 'o', and the type of 'o' is Object, so calling callMe(o) works.
[ March 09, 2008: Message edited by: marc weber ]
 
marc weber
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

Originally posted by Ram Manoj:
...Can you explain the following statement with an example.

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter...


In the code below, A is widest, and B, C, and D are each progressively narrower. The method is called using an instance of C. An exact match isn't found. D is narrower than C, so that method won't work. Both B and A are wider than C. But B is narrower than A, so B is invoked.

[ March 09, 2008: Message edited by: marc weber ]
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooh, this is it. Thankyou weber for clarifying me.

I was thinking it would be something else, the terminology got me stuck "argument that is wider than the parameter".

No more getting troubled by overloading questions.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic