For example if we have a class Person and a class Student extends Person.
The we may have:
public interface PersonQueryBuilder<T extends Person> {
PersonQueryBuilder<T> withName(String name);
PersonQueryBuilder<T> withAgeBetween(int from, int to);
List<T> getResultList();
}
public interface StudentRepository<T extends Student> extends PersonQueryBuilder<T> {
StudentRepository studying(Course course);
}
So why when I have an StudentRepository, both the withName() and withAgeBetween() methods return a PersonQueryBuilder and not and StudentRepository? This is very annoying. Is there any “elegant” way of solving this problem?
This is kind of funny. I propose the following (ugly?) solution that doesn’t require you to redefine the methods in the interface as Mark’s answer does:
Also you’re using raw types as results in your current implementations of your
withmethods.