This discussion is archived
2 Replies Latest reply: Feb 15, 2010 11:57 AM by 843793 RSS

How to define a single parameterized type with two constraints

843793 Newbie
Currently Being Moderated
Sorry if this has been asked, but searching for existing answers for this proved troublesome.

I am wondering if a parameterized type can be associated with more than one type. For example, suppose I want to ensure a parameterized type is of both Runnable and Cloneable. My quick intuition was to write this:
public class MyClass<T extends Runnable, Cloneable> { ... }
Which is obviously wrong because "Cloneable" falls into the 2nd parameterized type.

A quick-solution of this would be to create an interface that extends both Runnable and Cloneable and simply use that:
public class MyClass<T extends RunnableCloneable> { ... }
Clearly, though, if we walk down that road, we end up littering our libraries with one-time-use types. Is there a way without creating the combined type and using that?

Joe