In the following code example:
interface Eatable{ public void printMe();}
class Animal { public void printMe(){System.out.println("Animal object");}}
class Dog extends Animal implements Eatable{ public void printMe(){System.out.println("Dog object");}}
class BullTerrier extends Dog{ public void printMe(){System.out.println("BullTerrier object");}}
public class ZiggyTest{
public static void main(String[] args) throws Exception{
Object[] objArray = new Object[]{new Object(), new Object()};
Collection<Object> objCollection = new ArrayList<Object>();
Animal[] animalArray = new Animal[]{new Animal(),new Animal(),new Animal()};
Collection<Animal> animalCollection = new ArrayList<Animal>();
Dog[] dogArray = new Dog[]{new Dog(),new Dog(),new Dog()};
Collection<Dog> dogCollection = new ArrayList<Dog>();
System.out.println(forArrayToCollection(animalArray,animalCollection).size());
// System.out.println(forArrayToCollection(dogArray,dogCollection).size()); #1 Not valid
System.out.println(genericFromArrayToCollection(animalArray,animalCollection).size());
System.out.println(genericFromArrayToCollection(dogArray,dogCollection).size());
System.out.println(genericFromArrayToCollection(animalArray,objCollection).size()); //#2
System.out.println(genericFromArrayToCollection(dogArray,animalCollection).size()); //#3
// System.out.println(genericFromArrayToCollection(objArray,animalCollection).size()); //#4
}
public static Collection<Animal> forArrayToCollection(Animal[] a, Collection<Animal> c){
for (Animal o : a){
c.add(o);
}
return c;
}
static <T> Collection<T> genericFromArrayToCollection(T[] a, Collection<T> c) {
for (T o : a) {
c.add(o);
}
return c;
}
}
Why does the compiler allow the call to the genericFromArrayToCollection()
method only if the declared type of the collection is the parent of the declared type of the array (See lines marked #2, #3 and #4) . Why is this please?
Thanks
Edit
When i uncomment the line marked #4 i get the following error
ZiggyTest.java:34: <T>genericFromArrayToCollection(T[],java.util.Collection<T>) in ZiggyTest cannot be applied to (java.lang.Object[],java.util.Collection<Animal>)
System.out.println(genericFromArrayToCollection(objArray,animalCollection).size()); //#4
^
1 error
Edit 2
@Tudor i tried the following method using this statement
System.out.println(method1(new ArrayList<String>()).size());
The compiler complained with an error saying that cannot be applied to java.util.ArrayList
public static Collection<Object> method1(ArrayList<Object> c){
c.add(new Object());
c.add(new Object());
return c;
}