My question largely relates to this one Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
So, say we have Animal that is a super interface of Cat and Dog. We also have a abstract class Litter such that
public abstract class Litter{
public Litter(Collection<Animal> animals){}
}
And then we naturally have a concrete class KittyLitter
public class KittyLitter extends Litter{
public KittyLitter(Collection<Cat> animals) {
super(animals);
}
}
...and puppy litter.
Naturally, we want to limit all Animal in a KittyLitter to just Cat. Why doesnt Java allow us to do this? Then, also lets say we add another method --
public abstract void addCub(Animal animal);
and concrete implementation in KittyLitter of
@Override
public void addCub(Animal cat) {
// TODO Auto-generated method stub
}
At this point this breaks logic and allows us to insert a Dog into a KittyLitter which makes no sense. Any ideas as to why Java does these things to us? Also, if KittyLitter constructor can be changed to accept a List, why does the type argument behave differently? Can anyone explain why this is so?
EDIT: this is really not about constructors, but also any method that overrides.