In line 13, the version of the method that takes an int is called, and in line 14, the version that takes a String is called. To decide which of the two methods to call, Java looks at the number and type of the arguments that you are passing to it. Obviously, if you call it with String, only the version that takes a String applies. It would be strange if it would work differenly. What more justification do you need for this?
First i agree with Jasper, it is correct the matching methods
say if you have sum(int), sum(String) two methods then you can overload only two methods of the same name
like sum(5); and sum("prabhat");
here if you pass sum(12.5); this line will give you compile time error only.
I will give input on henry conclusion.
Joined: Oct 04, 2006
My reply on henry example.
Example e = new ExampleWithString();
e.method("Hello"); // Error. Even though instance has method.
in line no 16 it is giving Error because methods are overloaded into the same class only , am i right ?
e is the instance of ExampleWithString ,but compile time polymorphism works on the ref type not Object Type(instance) so compiler is trying to find and match the method with name method(with String type) only but it is defined in class Example.
So compiler gives the compile time method no matching method found.