File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Head First Java.Chapter 16. Be the Compiler.UPDATE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java.Chapter 16. Be the Compiler.UPDATE" Watch "Head First Java.Chapter 16. Be the Compiler.UPDATE" New topic
Author

Head First Java.Chapter 16. Be the Compiler.UPDATE

Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Hello, can anybody help me? I have been studying Head First Java. And i have met with some difficulties. Chapter 16.
Exercise "Be the Compiler". Determine which of these statements would compile.
Please, give me full and detailed explanations if it is possible for you)
So, i conducted research and what i have found:
At the beginning, initial data. Animal-superclass, Dog extends Animal, Cat extends Animal

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.
2)ArrayList<Animal> animals1 = new ArrayList<Dog>(); FALSE - Incomparable Types;
Reference variables of type <Animal> can't have all methods of subtypes.
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)

If i am misled, please point out errors. Great thanks for help!
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Why not try to compile them and see what the error message is?

If you don't understand why one won't compile, we can help you out.
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 71

Exactly, it may sound difficult at first but "Be the Compiler" and try the examples out. It will give you a through understanding of Collections. Any more doubts, come back to the ranch, help will always be available.


VM
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to the Ranch
I suggest you have a look at the Java Tutorials and look for the generics sections (there appear to be 2). In this part there is a generics and subtyping section. I think that is what that particular exercise is about.
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Mack Wilmot wrote:Why not try to compile them and see what the error message is?

If you don't understand why one won't compile, we can help you out.


What about to look at my post again?? Please, correct me if my reasoning are wrong.
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Vineeth Menon wrote:Exactly, it may sound difficult at first but "Be the Compiler" and try the examples out. It will give you a through understanding of Collections. Any more doubts, come back to the ranch, help will always be available.

Could you look this topic once more and write your opinion?
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

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)

 
 
subject: Head First Java.Chapter 16. Be the Compiler.UPDATE