I would like to clarify a few things about binding and dispatch.
Binding is attaching a method call to a method signature. Dispatch is choosing over method implementations. First comes binding, then comes dispatch. OK, I got this, but:
I've read lots of explanations over the last few days and it's a mess for me now. Some of them are conflicting or completely opposite from one another. From what I know all non-static, non-private and non-final methods in Java are virtual by default and must use dynamic binding and dynamic dispatch.
Binding is attaching a method call to a method signature. Dispatch is choosing over method implementations.
Binding in the sense you mean refers to overloading. Dispatch in the sense you mean refers to overriding.
First comes binding, then comes dispatch.
Yes. The compiler does the binding, the JVM does dispatching.
OK, I got this, but:
- Do instance methods that have not been overridden use static binding?
No. The compiler can't tell that, except in the case of private methods, in which case it uses the invokespecial bytecode.
- Why exactly do overloaded methods use static binding since they are virtual and can be overridden in subclasses?
They don't.
From what I know all non-static, non-private and non-final methods in Java are virtual by default and must use dynamic binding and dynamic dispatch.
Correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With