Robert Raps wrote:
1)ArrayList<Dog> dogs1 = new ArrayList<Animal>(); FALSE- Incomparable Types;
What i think. We can add to ArrayList<Animal>every subtypes of Animals, but reference variables are type of Dog so they can't refer to the methods and instance variables brothers of Dog, for instance Cat.
The types are not compatible because they are two completely different types ArrayList<Dog> and ArrayList<Animal>, not because Animal is not a subtype of Dog. If it was because Animal is not a subtype of Dog, then the next line would compile. As you can see on the next line, ArrayList<Dog> is not a subtype of ArrayList<Animal> although Dog is a subtype of Animal. So your reasoning as to why is not correct.
Robert Raps wrote:
2)ArrayList<Animal> animals1 = new ArrayList<Dog>(); FALSE - Incomparable Types;
Reference variables of type <Animal> can't have all methods of subtypes.
Here again the reason they are not compatible is because the types ArrayList<Animal> and ArrayList<Dog> are not compatible. Animal and Dog are compatible because you can assign a Dog type to an Animal reference, but you can't assign an ArrayList<Dog> to an ArrayList<Animal> reference.
The same goes for the rest of the incompatible types. The type is the full "Type<Type Argument>" not the Type Argument itself.
As you can see below a Generic Supertype/Subtype with the same Type Parameter are compatible. Another example would be AbstractCollection<Dog> col = new ArrayList<Dog>(); works because ArrayList<E> is a subtype of AbstractCollection<E>.
Robert Raps wrote:
3)List<Animal> list = new ArrayList<Animal>();TRUE - All ok. I understand.
4)ArrayList<Dog> dogs = new ArrayList<Dog>(); TRUE- All ok.I understand.
5)ArrayList<Animal> animals = dogs; FALSE-- Incomparable Types; Look (2)
6)List<Dog> dogList = dogs; TRUE-- All ok. I understand.
7)ArrayList<Object> objects = new ArrayList<Object>(); TRUE- All ok. I understand.
8)List<Object> objList = objects; TRUE- All ok. I understand.
9)ArrayList<Object> objs = new ArrayList<Dog>(); FALSE -Incomparable types; Look (2)