Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending both T and SomeInterface<T> in Java

I want to create a class that takes two parameters. One should be typed simply as T. The other should be typed as something that extends both T and SomeInterface<T>. When I attempt this with

public class SomeClass<T, S extends SomeInterface<T> & T>

then Java complains with

"The type T is not an interface; it cannot be specified as a bounded parameter"

and if instead I attempt to create an interface for S with

public interface TandSomeInterface<T> extends SomeInterface<T>, T

then Java complains with

"Cannot refer to the type parameter T as a supertype"

Is there any way to do this in Java? I think you can do it in C++...?

like image 896
Graeme Moss Avatar asked Mar 30 '10 07:03

Graeme Moss


1 Answers

You can't create an interface that extends the type parameter T since there's no contract that would guarantee T to be an interface. And of course interface extending a class is not allowed.

like image 50
hleinone Avatar answered Oct 12 '22 23:10

hleinone