Java overloading rules -


i came across 2 overloading questions can't find answer , don't have java environment run test code. i'm hoping can me assembling list of rules java compilers follow follow overloading or alternately pointing me @ list exists.

first, when 2 methods differ final varargs parameter, under circumstances each called , can call varargs method without args?

private void f(int a) { /* ... */ } private void f(int a, int... b) { /* ... */ }  f(12); // calls former? expect f(12, (int[])null); // calls latter, passes null b?    // can force compiler call second method in same fashion   // happen if first method didn't exist? 

second question, when 2 methods differ types inherited 1 gets called? i'd expect derived version called, , casting allowed call other.

interface {} class b implements {} class c implements {}  private void f(a a) {} private void f(b b) {}  f(new c()); // calls first method f(new b()); // calls second method? f((a)(new b()); // calls first method using b object? 

these 2 examples, code-reader i'd prefer canonical list of exact ordered rules used resolving don't have time setup build environment check compiler doing.

overloading vs overriding

the selection of right implementation of method done @ runtime pointed out, signature of method invoked decided @ compile time.

overloading method selection @ compile time

the java language specification (jls) in section 15.12 method invocation expressions explains in detail process compiler follows choose right method invoke.

there, notice compile-time task. jls says in subsection 15.12.2:

this step uses name of method , the types of argument expressions locate methods both accessible , applicable there may more 1 such method, in case specific 1 chosen.

typically, varargs methods last chosen, if compete other candidate methods, because considered less specific ones receiving same parameter type.

to verify compile-time nature of this, can following test.

declare class , compile it.

public class choosemethod {    public void dosomething(number n){     system.out.println("number");    } } 

declare second class invokes method of first 1 , compile it.

public class methodchooser {    public static void main(string[] args) {     choosemethod m = new choosemethod();     m.dosomething(10);    } } 

if invoke main, output says number.

now, add second more specific overloaded method choosemethod class, , recompile (but not recompile other class).

public void dosomething(integer i) {  system.out.println("integer"); } 

if run main again, output still number.

basically, because decided @ compile time. if recompile methodchooser class (the 1 main), , run program again, output integer.

as such, if want force selection of 1 of overloaded methods, type of arguments must correspond type of parameters @ compile time, , not @ run time.

overriding method selection @ run time

again, signature of method decided @ compile time, actual implementation decided @ runtime.

declare class , compile it.

public class choosemethoda {    public void dosomething(number n){     system.out.println("number a");    } } 

then declare second extending class , compile:

public class choosemethodb extends choosemethoda {  } 

and in methodchooser class do:

public class methodchooser {     public static void main(string[] args) {         choosemethoda m = new choosemethodb();         m.dosomething(10);     } } 

and if run output number a, , ok, because method has not been overriden in choosemethodb , therefore implementation being invoked of choosemethoda.

now, add overriden method in methodchooserb:

public void dosomething(number n){     system.out.println("number b"); } 

and recompile one, , run main method again.

now, output number b

as such, implementation chosen @ runtime, , not recompilation of methodchooser class required.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -