Sorry if the title sucks, not sure how to ask this question...
Lets say I have some interface Foo that takes a parameter
public interface IFoo<Bar extends IBar>{
public Bar generateBar();
}
and I have another class that takes a foo object and will generate a bar object. I can define it like this.
public class FooBar<Bar extends IBar, Foo extends IFoo<Bar>>{
private Foo foo;
public FooBar(Foo foo){
this.foo = foo;
}
public Bar getBar(){
this.foo.generateBar();
}
This all works fine. However, it seems silly for me to be defining Bar as a parameter of my FooBar class. The IFoo object is going to be parametrized to take the Bar value, so I would assume I could infer the type of Bar from whatever parameter the Foo object has; thus avoiding someone having to provide two parameters every time they define a FooBar object.
Is there a way I can do this? Instead of making Bar part of the FooBar parametrized type just infer it from Foo's parameter?
edit: The answer appears to be this can't be done, as already answered. However, I'm going to leave this open for another day because I'm hoping someone may post an answer that not only if it can be done, but why it is not possible. Specifically, is this simply a use case java developers never chose to support, or is there a reason that it's not possible for the same compile time type checking that supports other parametrization checks to infer Bar type by looking at the specific IFoo type parameter. isn't IFoo parameter able to be determined as well as any parametrized type by the compiler?