I’m new to Java and I have struck with some doubt. For example, consider the expression like:
a.method()
a.method("string")
people call them “dynamic dispatch”. But I’m sure that the type checker makes sure that the methods named method(),method(String a) available for object a.
But why does it called “dynamic”? It isn’t static call? Since the compiler found that already?
The example that you have posted will not use dynamic dispatch. You have posted an example of
Method Overloading. And decision of method invocation in case ofOverloadingis done at compile time. It is on the compiler to decide which method will be invoked based on theformal parametersand theactual argumentspassed.Dynamic Bindingcomes into play when you are working withMethod Overriding, where the decision of which method will actually be invoked is delayed till runtime.For e.g: –
The decision of which method is invoked is decided on the basis of which class instance the particular reference is pointing to, And that is only known at
runtime. And henceDynamic Dispatch.