Generally speaking, is it possible to restrict the classes that may implement an interface?
More specifically, can a generic interface Foo<T>
restrict its implementations to descendants of T
:
interface Foo<T> {}
class Baz extends Bar implements Foo<Bar> {} // desirable
class Baz extends Bar implements Foo<Qux> {} // undesirable
The context is that Foo<Bar>
objects should be castable to Bar
objects in a type-safe way.
Having exhausted all other sources of information, I already have a strong hunch that this isn't possible—but I would be delighted if someone could prove otherwise!
T getT()
method be sufficient? An implementation could implement it asreturn this;
if it happens to be of the correct type.