jvm - Method Binding in Java -
when reading thinking in java (4th edition) recently, got problem method binding in java. first let's @ 2 definitions book:
- connecting method call method body called binding.
- all method binding in java uses late binding unless method static or final.
you can find definitions in section method-call binding chapter polymorphism. (page 281-282)
to testify that, wrote following code:
public class test3{ public static void main(string[] args) { bindingtest_sub sub1 = new bindingtest_sub(); bindingtest_base sub2 = new bindingtest_sub(); sub1.ovrld(new integer(1)); // statement 1 sub2.ovrld(new integer(2)); // statement 2 sub2.ovrrd(); // statement 3 } } class bindingtest_base { void ovrld(object obj){ system.out.println("bindingtest_base ovrld()"); } void ovrrd(){ system.out.println("bindingtest_base ovrrd()"); } } class bindingtest_sub extends bindingtest_base{ void ovrld(integer i){ system.out.println("bindingtest_sub ovrld()"); } void ovrrd(){ system.out.println("bindingtest_sub ovrrd()"); } }
the execution result is:
bindingtest_sub ovrld() bindingtest_base ovrld() bindingtest_sub ovrrd()
based on result, have following questions:
- according definition book, since of methods inheritable, java use late binding (dynamic binding) 3 statements. however, read other articles, said java use static binding when dealing overloading. seems contradictory because statement 1 overloading.
- i not understand why java called ovrld() of base class in statement 2. if used dynamic binding, should call overld() of sub class because runtime jvm should clear sub2 instance of bindingtest_sub class. on other hand, if used static binding, should call overld() of sub class because compiler able observe type of given argument integer. can tell me job has been done compiler or jvm when deals statement 2.
- the outcome of statement 3 makes sense me. still, curious how compiler recognize (ovrrd()) overriding method. in words, how compiler know there class has method overrides ovrrd().
any thoughts above questions or java method binding mechanism appreciated. please feel free point out mistakes.
tl;dr; you're not overloading ovrld(object)
, not in runtime. compiler uses compile time type information decide best virtual method call. sub1
, sub2
have different compile time types. , sub1
's type has different best match ovrlb(integer)
.
explaining. you're wondering:
- if
sub1.ovrld(new integer(1))
callsbindingtest_sub.ovrld(integer)
- then why
sub2.ovrld(new integer(2))
callingbindingtest_base.ovrld(object)
in case works this:
the compiler uses compile time type information of variable decide method call. compile time type of sub2
bindingtest_base
, in runtime assigning bindingtest_sub
it, that's not relevant compiler.
the method bindingtest_base matches parameters of call is: bindingtest_base.ovrld(object)
so compiler issues virtual call orvld(object)
now, runtime method virtual call decided based on full signature of method being invoked (name + parameters). , there no overload ovrld(object)
in bindingtest_sub
base class method invoked.
with sub1
compiler has more information. sub1
's compile time type bintdingtest_sub
, there method matches ovrld(integer)
in class.
looking @ bytecode can see clearly:
aload 1 // sub1 // ... blah blah blah creating integer // last opcode issued compiler "statement 1" invokevirtual com/ea/orbit/actors/samples/helloworld/bindingtest_sub.ovrld (ljava/lang/integer;)v aload 2 // sub2 // ... blah blah blah creating integer // last opcode issued compiler "statement 2" invokevirtual com/ea/orbit/actors/samples/helloworld/bindingtest_base.ovrld (ljava/lang/object;)v
Comments
Post a Comment