J
Joe Fox
I'm reading a book called "Head First Java," and there is an exercise
in the book (p. 576) which asks if some code will compile, and there
appears to be some errors in the given answers, because my compiler
(Eclipse) doesn't agree with the book's answers (The answers assume a
5.0 compiler, which is in fact what I'm using.). I'm interested in
getting feedback from others about whether the book's answers are
incorrect or Eclipse is somehow wrong.
The questions are based on the following classes:
abstract class Animal {
// irrelevant
}
class Dog extends Animal {
// irrelevant
}
The book claims that the following are all legal declarations; Eclipse
seems to think that none of them are:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog> ();
ArrayList<Object> animals = new ArrayList<Dog> ();
I find this confusing, because I thought that the second two should
compile but the first one shouldn't; Eclipse won't let any of them
compile; and the book says all of them should compile. Sigh. It
shouldn't be this difficult.
So, there are two issues here: 1) why is the book disseminating
incorrect information, and 2) why don't the last two compile when it
seems to me that they should? I'm only a newbie, and don't really know
anything, so any help is appreciated. Thanks!
in the book (p. 576) which asks if some code will compile, and there
appears to be some errors in the given answers, because my compiler
(Eclipse) doesn't agree with the book's answers (The answers assume a
5.0 compiler, which is in fact what I'm using.). I'm interested in
getting feedback from others about whether the book's answers are
incorrect or Eclipse is somehow wrong.
The questions are based on the following classes:
abstract class Animal {
// irrelevant
}
class Dog extends Animal {
// irrelevant
}
The book claims that the following are all legal declarations; Eclipse
seems to think that none of them are:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog> ();
ArrayList<Object> animals = new ArrayList<Dog> ();
I find this confusing, because I thought that the second two should
compile but the first one shouldn't; Eclipse won't let any of them
compile; and the book says all of them should compile. Sigh. It
shouldn't be this difficult.
So, there are two issues here: 1) why is the book disseminating
incorrect information, and 2) why don't the last two compile when it
seems to me that they should? I'm only a newbie, and don't really know
anything, so any help is appreciated. Thanks!