About error using Java generics: “type parameter S is not within its bound”

前端 未结 3 962
傲寒
傲寒 2020-12-20 14:06

I am writing some classes using Generics but I can\'t find a solution for the class SolutionsSubset and so I a getting the error \"type parameter S is not within its bound\"

相关标签:
3条回答
  • 2020-12-20 14:40

    In order to be used as the type argument in MathSubset, SolutionsSubsets S must extend Comparable<S>. As a compilable example:

    import java.util.TreeSet;
    
    interface Subset<T extends Comparable<T>>
         extends Comparable<Subset<T>> { }
    
    class MathSubset<T extends Comparable<T>>
        extends TreeSet<T>
        implements Subset<T>
    {
        public int compareTo(Subset<T> other) { throw new Error(); }
    }
    
    interface Solution<T extends Comparable<T>> { }
    
    interface Solutions<S extends Solution<?>> extends Iterable<S> { }
    
    class SolutionsSubset<S extends Solution<?> & Comparable<S>>
        extends MathSubset<S>
        implements Solutions<S>
    { }
    

    A few comments: This is very abstract example, and so not easy to think about. Laying out the code so you don't need to scroll is good. There's an awful lot of inheritance going on here, perhaps compose rather than, say, extending TreeSet. It's difficult to distinguish between the identifiers Solutions and Solution.

    0 讨论(0)
  • 2020-12-20 14:42

    Generics are something that can quickly get out of hand, especially if you try to "be all generic" all at once. Less is more. What always helps me is to start concrete (including the implementation) and then slowly substitute generic parameters in, one parameter and class at a time.

    Could anybody help me to improve my knowledge about generics?

    http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

    Not a tutorial, but lots of useful info. Its one of those references that you read the parts you can understand, but come back to over and over again in the future as you gain more mastery and more of it begins to make sense.

    0 讨论(0)
  • 2020-12-20 14:44

    First of all, here is the full error (which is specific to MathSubset not getting a proper parameter): Bound mismatch: The type S is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type QifFixer.MathSubset<T>

    The problem is that MathSubset expects a <T extends Comparable<T>, but you're giving it a S extends Solution<?> - those types having nothing to do with each other, because a Solution does not inherit or implement Comparable<T>.

    If anything, you could try this:

    public class SolutionsSubset<S extends Comparable<S>> extends
        MathSubset<S> implements Solutions<Solution<S>>;
    

    Unfortunately, this will STILL not work because MathSubset implements Iterable, but so does Solutions.

    An easy fix would be for Solutions to not extend Iterable, but it really sounds to me like you're trying to use a more complex approach than you need to. May be a "has-a" instead of "is-a" design might be more beneficial here?

    0 讨论(0)
提交回复
热议问题