Well actually Nitesh, this has nothing to do with co-variant return types.
Co-variant return types are used when you are
overriding a method, and are narrowing the return type. For example, what Sun should have done with java.util.Date:
Because any calling method expects an Object, a Date object will be assignable.
The trick I mentioned only works with methods with generics, and involves the actual method signature in byte code.
The only reason why the compiler doesn't allow methods with the same name and parameter types but different return types without generics is ambiguity - it doesn't know which one to choose. With generics the ambiguity is not there* and the compiler can fill in the details.
With the following methods:
the actual method signatures for the JVM are different:
* Of course if both Maps are Map<
String,String> there is still ambuigity, but you get the picture.
[ December 08, 2008: Message edited by: Rob Prime ]