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

Method overloading

Collin Dugas
Ranch Hand

Joined: Jul 10, 2009
Posts: 91
I was expecting some kind of exception at runtime from the below code but to my surprise the code ran without any issues and the output was
"SubClass". From testing similiar code I understood that it chooses the method that takes argument which appears more lower in the inheritance tree. It should work fine if some super class/sub class reference was passed to it but how does it decide with null?
It might be pretty straightforward to some but I did not get it. Please help me understand this.


Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
I was expecting some kind of exception at runtime


Overloading is completely decide by compiler so nothing remains for runtime.

You yourself explained that the compiler chooses exact or the most narrower match for a method call
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Search for "most specific" in this forum and you will find several answers.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Rob Prime wrote:Search for . . .
Or search the Java™ Language specification.
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
and yes, you are not doing any kind of operation on objects in method "printSomething" so exception will not come from any way.


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Collin Dugas
Ranch Hand

Joined: Jul 10, 2009
Posts: 91
Manish Singh wrote:
I was expecting some kind of exception at runtime


Overloading is completely decide by compiler so nothing remains for runtime.

You yourself explained that the compiler chooses exact or the most narrower match for a method call


Thanks for the explanations. After trying out samples I understood how it works.

But my doubt was something like this.

It is decided at compile time which method is to be called. And it decides on the base of the parameters to call the method having the most specific reference . So if I have a reference of the superclass then the method taking superclass as an argument is called. Thats fine. But with null how does it decide which is the method taking the most specific reference as an arg.
Another way I was putting it was for anymethod being called the arguments passed would pass the instanceof method check.
Does it mean that null is of the subclass type?

Please bear with me if you think my doubts are not worthwhile.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

No, null is NOT of the subclass type, but the "most specific" implementation is selected.

Try overloading a method with a String or Integer parameter and invoking that method with a null argument. The compiler error may make things clearer for you (or not)


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method overloading