Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading doubt

 
Ram Manoj
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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!)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic