aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading doubt" Watch "overloading doubt" New topic
Author

overloading doubt

Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
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

Joined: Aug 31, 2004
Posts: 11343

Have you seen our SCJP FAQ: What is a most-specific method? Note that the last line of that explanation addresses null references.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
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

Joined: Jan 12, 2008
Posts: 52
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 12, 2008
Posts: 52
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
 
subject: overloading doubt