I have a question about generics in Java that I can't seem to find the answer to. Here is my current code:
interface ISelect<T>{
// a predicate that determines the properties of the given item
public boolean select(T t);
}
class BookByPrice<T> implements ISelect<T> {
int high;
int low;
public BookByPrice(int high, int low) {
this.high = high;
this.low = low;
}
public boolean select(T t) {
return t.getPrice() >= this.low && t.getPrice() <= this.high;
}
}
So, basically, I have to define this class BooksByPrice that implements the interface ISelect and acts as a predicate to be used in a filter method in another interface of classes that acts as a list implementation. BooksByPrice is supposed to have this method select that returns true if a book's price is between low and high. The entire body of the BooksByPrice class is subject to change, but the interface must remain as it is in the code. Is there some way to instantiate the generic type T in the class BooksByPrice so that it can use the methods and fields of a book? Otherwise I see no reason that the select method has a generic as a parameter.
Thanks for any help.
BookByPrice. You can simply declare it asclass BookByPrice implements ISelect<BookByPrice>.