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{
....
Unfortunately this cannot be enforced by Java's type system.
You can get pretty close, though, by using:
public interface Bar> {
List getFoo();
}
And then your implementing classes can implement it like so:
public class SomeSpecificBar implements Bar {
// Compiler will enforce the type here
@Override
public List getFoo() {
// ...
}
}
But nothing stops another class from doing this:
public class EvilBar implements Bar {
// The compiler's perfectly OK with this
@Override
public List getFoo() {
// ...
}
}