I have a java interface like this
public interface MyInterface<T> { public <V extends T> V get(String key, Bundle bundle); }
note the parameter of type <V extends T> method.
Then I have a class MyFoo implements MyInterface
class MyFoo implements MyInterface<Object> { // Object because can be any type @Override public <V> V get(String key, Bundle bundle) { return new Other(); } }
So, when I now have a class:
class Bar { public Other other; public Other setOther(Other other); }
Then I want to take MyFoo to set Other to an instance of Bar :
MyFoo foo = new MyFoo(); Bar bar = new Bar(); bar.other = foo.get();
It works great. Type can be determined using java generics. No additional cast is required.
However, if I want to use bar.setOther() , then the type cannot be determined:
MyFoo foo = new MyFoo(); Bar bar = new Bar(); bar.setOther(foo.get());
Then I get the following compilation error:
error: incompatible types: object cannot be converted to another
I do not understand why this does not work for the bar.setOther( foo.get() ) method, but it works with direct access to the bar.other = foo.get() field
Any idea how to solve this problem without adding an extra listing like bar.setOther( (Other) foo.get() )