How do I enforce that the method getFoo() in the implementing class, returns a list of the type of same implementing class.
public interface Bar{
....
List<? extends Bar> getFoo();
}
Right now a class that implements Bar returns objects of any class that implements Bar. I want to make it stricter so that the class that implements Bar returns a List of objects of only its type in getFoo().
Unfortunately this cannot be enforced by Java’s type system.
You can get pretty close, though, by using:
And then your implementing classes can implement it like so:
But nothing stops another class from doing this: