• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overloading

 
Collin Dugas
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for "most specific" in this forum and you will find several answers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Search for . . .
Or search the Java™ Language specification.
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and yes, you are not doing any kind of operation on objects in method "printSomething" so exception will not come from any way.
 
Collin Dugas
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic