Java generic class that contains an instance of implementation of generic interface

100 views Asked by At

I am developing an independent, self contained component, that needs domain specific parts to function properly. The part of the idea is to create a generic interface, that will settle the usage of interface's implementation in another part of this component (in my example in class B).

I have written the following example for the generic interface:

public interface A<T> {
    public A f0(T t);
    public T f1();
}

API consumers will have to create an implementation of this interface and pass the instance of it to the component. The component will then create an instance of the following class B, which will contain the input instance of the interface's implementation:

public class B<T extends A> {
    private T t;

    public X getTsSomething(){
        return t.f1();
    }
}

Question:

How could I force X to be the same type as type parameter in the interface A? Is there an obvious solution or is this just the result of a bad design? If so, what would be a better approach?

1

There are 1 answers

0
Eran On BEST ANSWER

It looks like what you want is :

public class B<S,T extends A<S>> {
    private T t;

    public S getTsSomething(){
        return t.f1();
    }
}