How
far do Java's polymorphism and dynamic method dispatch go? A
long time ago, early in my Java-life, I made an impromptu, informal bet
with a couple of fellow programmers about Java's level of dynamicity (is
that a word?). The question was this: Does Java use the assigned type
or the declared type of variables used as method arguments when doing
method lookup?
This
is called late binding, because it occurs as late as possible in the life
of the object. I was wondering if Java behaves similarly when considering
method arguments. In other words, is method lookup done dynamically (at
runtime) with respect to method arguments, or is it done statically at
compile-time? My experiments surprised me at the time, and it turns out
the bet was a push.
...what are the results of each of the following calls to aMethod()? Test aTest = new Test(); Superclass aSuper = new Superclass(); Subclass aSub = new Subclass(); As it turns out, only numbers 1, 2, 5, and 6 do the method lookup like I expected. #3 and #4 both invoke aMethod(Superclass) even though the object being passed in is (at runtime) a Subclass. Most interestingly, #7 invokes aMethod(Subclass), apparently because the compiler goes to the version that takes the narrowest argument type it can find (based on some further experimentation I performed). |
All photographs and content is copyright © 2003-2006 Eric Rizzo & Jazmine Rizzo |