Take a look at these three classes. Minatchi allows itself to be extended so that its methods' returning type could be extended as well. To illustrate, I used a static method.
public class Minatchi<T extends Minatchi<?>>{
static public <T extends Minatchi<?>>
List<T> listAll(){
return
(List<T>) query();
}
}
And so I subclass Minatchi into Lady
public class Lady
extends Minatchi<Lady>{
}
This is where the questionable behaviour takes place.
public class HelloMinatchi{
private void listA(){
List<Lady> uvs = Lady.listAll();
for (Lady uv: uvs){
logger.info(uv.getName() );
}
}
private void listB(){
for (Lady uv: Lady.listAll()){
logger.info(uv.getName() );
}
}
}
Methods listA and listB are essentially the same. listA places the list into an intermediate variable uvs, whereas listB directly places listAll into the for-loop header.
However, for listB, the compiler complains Cannot convert Minatchi<?> to Lady.
So this question is about the design integrity of Java generics. Yet another generics gripe.
Is this a deliberate design feature or an unintentional design bug that Java generics designers did not know how to solve. If deliberate, why did they do that? If bug, are they planning to solve it?
Or is this my personal problem that I do not know a better way to declare the generics? If so, tell me how.
(I used a generic Minatchi class because I have non-static methods to be exposed to class extension too, which I left out in the question.)