File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A good one on overloading... . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A good one on overloading... ." Watch "A good one on overloading... ." New topic
Author

A good one on overloading... .

Rafa Barcel� Bauz�
Greenhorn

Joined: Aug 09, 2006
Posts: 22
Hello.
I have a little doubt on overloading methods...

Why is m(ArrayList a) executed?, Is because it's more inheritance especific the parameter to m method or something like that???

public class Pruebas {

public static void main(String[] args) {
new Pruebas().m(null);
}

public void m(Object o) {
System.out.println("o");
}

public void m(ArrayList a) {
System.out.println("s");
}

}

***************************************************

After this, I put another method like this:

public void m(LinkedList l) {
System.out.println("l");
}

and the compiler says:

The m(Object) is ambiguous.

I supose it's because LinkedList ant ArrayList are in the same
inheritance level.

Can someone explain me how overloading works in a situation like this??

Thanks!!!
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hi,


The overloaded method which has smallest argument having width that is enough for the calling parameter (if the argument type is primitive) or the refernce type down in the inheritance tree which fits the calling parameter,is a right candidate for this overloaded method invocation.Hence the method m(ArrayList a) will be called.


LinkedList and ArrayList are the implementation of same interface so they are sibling (which can not be checked by the compiler,I guess),not sub-class /super-class.So when the compiler sees such type of code it feels the amiguity for not having the answer for the question "which one to invoke,both are eligible?",so stops compilation.

Correct me if Im wrong.


~Sanjeev Singh<br />SCJP 1.5
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
"How methods get resolved when executing an overloaded method" links.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Rafa Barcel� Bauz� posted November 02, 2006 04:33 AM

Why is m(ArrayList a) executed?, Is because it's more inheritance especific the parameter to m method or something like that???



Exactly. This behaviour is also true for primitive values.

What I found a little tricky is that the specificity only looks for the reference type, not for the object type. So with a polyporph variable you can run into a trap:


Point is more specific than an object, but only the reference type counts here.
so the Object mixed will go into the Object method. And "Object" is printed out.



Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A good one on overloading... .