How to restrict Java generics type parameter in interface to certain classes -


i creating typed interface defintion , want restrict possible values type parameter set of classes. these classes existing , not under control. also, not related each other through class hierarchy.

so, example, have 3 classes a, b, c. interface imyfancyinterface<t>. how can restrict implementors of interface , make sure t either a, b, or c only? lot.

cheers,

martin

if a, b , c have common super-type (let's it's called super), do:

public interface imyfancyinterface<t extends super> { .. } 

this way should implement interface type-parameter sub-type of super, i.e. a, b or c.

if, however, a, b , c don't have common super-type, create marker interface (an interface no abstract methods) , make them implement it. example:

public interface marker { }  public class implements marker { }  public class b implements marker { }  public class c implements marker { } 

this way you'd able follow approach suggested:

public interface imyfancyinterface<t extends marker> { .. } 

Comments