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?
interface StudentRepository<T extends Student> extends PersonQueryBuilder<T> {
StudentRepository<T> studying(Course course);
StudentRepository<T> withName(String name);
StudentRepository<T> withAgeBetween(int from, int to);
}
You can redeclare the methods in the sub-interface. Implementors of StudentRepository will now be required to return a StudentRepository for those methods, but the implementation can still be used polymorphically as a PersonQueryBuilder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With