Hi i have a query in below code.
When i declare list of animals it allows me to put Cat() and Dog() in it as they both are animal and satisfy IS A relation ship.
Class Cat extends Animal{}
Class Dog extends Animal{}
List<Animal> list = new ArrayList<Animal>();
list.add(new Dog());//Code works fine
list.add(new Cat());//Code works fine
Now here is my question if ? extends
means accept anything that is subclass of Animal, then why is following code not compiling.
List<? extends Animal> list = new ArrayList<Animal>();
list.add(new Dog());//Compilation fails
list.add(new Cat());//Compilation fails
Same question , why dog is not accepted here ?
List<? extends Animal> dogs1 = new ArrayList<Dog>();
dogs1.add(new Dog());//Compilation fails
Can some one explain this behavior ?