You are confusing dynamic invocation with dynamic binding..
The first one allows the type checker to accept programs in which you are not sure if a method will be present on an object at run-time, while dynamic binding just chooses the right implementation according to the runtime type of the object but maintaining the statically type checking.
What does it mean?
It means that in your example, Java will call the implementation on object B
because the runtime type of the objA
variable is B
; and it will compile because it knows that a B
is a A
so the method invocation won't fail at runtime (objA
will have a F
implementation for sure).
With dynamic invocation instead it won't check at compile time that the type of the object on which you are calling F
contains that method, of course it will raise an exception if during execution the method won't be available on specified object.
Just for trivia: the invokedynamic
feature will be added with Java7 because many scripting languages have been written to work on top of JVM and the lack of a dynamic invocation feature forced the developers of these languages to add a middle layer between the script and the real JVM that cares about dynamic invocation using the reflection. Of course this approach causes a lot of overhead (think about Grovvy's MetaClass
), that's why Sun decided to give them a help..