2 Replies Latest reply on Feb 15, 2010 7:57 PM by 843793

    How to define a single parameterized type with two constraints

      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?