How do overloaded methods work?

most specific method argument is chosen for overloaded methods

In this case, String is subclass of Object. Hence String becomes more specific than Object. Hence Inside String method is printed.

Directly from JLS-15.12.2.5

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

As BMT and LastFreeNickName have correctly suggested, (Object)null will cause overloaded method with Object type method to be called.

Leave a Comment