1 Reply Latest reply on Sep 7, 2010 6:03 PM by 843793

    'Bound mismatch' error

      I have the following:
      public abstract class General <T extends General<T>>
      public class Particular1 extends General<Particular1>
      public abstract class AbstractSequence<T extends General<T>> extends
                General<AbstractSequence<T>> implements Collection<T>
      public class Particular2 extends AbstractSequence<Particular1>
      public class Particular3 extends AbstractSequence<Particular2>
      I get the following error:

      Bound mismatch: The type Particular2 is not a valid substitute for the bounded parameter <T extends General<T>> of the type AbstractSequence<T>

      But not if I do this:
      public class Particular3 extends AbstractSequence<AbstractSequence<Particular1>>
      I'm not sure exactly why, since after all Particular2 extends AbstractSequence<Particular1>.

      The class AbstractSequence is supposed to be a 'sequence of General' type class. In the end the right answer is probably simply to make this an interface, say Sequence. This is annoying if it implements Collection, because then as soon as I want to implement Sequence, I need to implement a bunch of annoying unsupported Collection methods - but it doesn't really have to implement Collection. Nevertheless I'm curious as to why the other approach doesn't work.
        • 1. Re: 'Bound mismatch' error
          When you declare
          class Particular3 extends AbstractSequence<Particular2>{}
          You're saying that T is Particular2. Particular2 must therefore extend General<Particular2> but it doesn't, it extends General<AbstractSequence<Particular1>> which is not the same thing (the former is less accepting than the latter).

          The fact that I had to stare at this for 5 minutes is probably an indicator that this is not a great use of generics. Though if maybe you had given some real-world names I would have been able to see where you were coming from.