An error using Java generics: "type S parameter is not within its bounds"

I am writing some classes using Generics, but I can’t find a solution for the SolutionsSubset class, and therefore I get the error "type S parameter is not within its boundary". I read the previous questions about the same error, but I cannot solve this for my case. Can someone help me improve my knowledge of generics? Any link to a good book (I can find a lot of information on google but if someone can recommend a book, tutorial, etc., we will be happy). Although I tried to keep in mind the rules to ask a question, but I'm sorry if my question does not comply with these rules.

I have the following classes and interfaces:

public interface Subset<T extends Comparable<T>> extends Comparable<Subset<T>> public class MathSubset<T extends Comparable<T>> extends TreeSet<T> implements Subset<T> public interface Solution<T extends Comparable<T>> public interface Solutions<S extends Solution<?>> extends Iterable<S> public class SolutionsSubset<S extends Solution<?>> extends MathSubset<S> implements Solutions<S> 

I need Subset to expand Comparable. In SolutionsSubset, the MathSubset class stores Solution objects. How do I change this definition to make it work?

Thank you in advance

+6
java generics syntax-error
source share
3 answers

To be used as a type argument in MathSubset , SolutionsSubset S must extend Comparable<S> . As a compiled 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 a very abstract example, and therefore not easy to think about. Code highlighting, so you don’t need to scroll, it’s good. Here, an awful lot of legacy may have been compiled, rather than, say, a TreeSet extension. It is difficult to distinguish between the identifiers Solutions and Solution .

+6
source share

First of all, here is the complete error (which does not match the correct MathSubset 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 <T extends Comparable<T> , but you give it S extends Solution<?> - those types that have nothing to do with each other because the solution does not inherit or does not implement Comparable<T> .

If anything, you can try the following:

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

Unfortunately, this will not work STILL, because MathSubset implements Iterable, but also a solution.

A simple solution would be for Solutions, so as not to extend Iterable, but it really sounds to me as if you are trying to use a more complex approach than you need. Maybe "has-a" instead of the "is-a" design might be more useful here?

+1
source share

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

Can someone help me improve my knowledge of generics?

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

Not a textbook, but a lot of useful information. Its one of those links that you read that you can understand, but come back again and again in the future when you gain more skill, and much more starts to make sense.

+1
source share

All Articles