I have a Generic interface (Pack<A extends PackAnimal>
) with one method that returns List<A>
. Today I've found that if in a class that implements the interface I forget to speficy the class (class XXX implements PackAnimal
) the return type is not checked in compilation time and fail during execution
interface PackAnimal {
}
class Buffalo implements PackAnimal {
}
interface LonelyAnimal {
}
class Puma implements LonelyAnimal {
}
interface Pack<A extends PackAnimal> {
List<A> getMembers();
}
class PumaPack implements Pack {
@Override
public List<Puma> getMembers() {
return null;
}
}
Why is that? How could I force that if there any type of mistake in the declaration the compilation will fail?