Dispatch, overloads and default methods
Definition:
Overload selection chooses the callable signature; dynamic dispatch chooses its overriding implementation.
Explanation:
Overload resolution runs at compilation, while an applicable instance override dispatches at runtime.
Illustration image:
Example:
static String select(long v) { return "widen"; }
static String select(Integer v) { return "box"; }
// select(1) returns "widen".Expected behavior. The literal is int, so the primitive-widening overload is found before a boxing invocation is needed.
Internal working:
Widening can win before boxing; varargs is a later applicability phase. Static methods and fields do not participate in dynamic dispatch. A class implementation wins over an inherited interface default, and independent conflicting defaults need an override.
Interview questions:
Which happens first for a Number reference holding an Integer?
The compiler selects the overload using declared types; runtime dispatch then selects the receiver’s override of that signature.