Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robert Raps
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic