• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics exercise in Head First Java 2nd ed.  RSS feed

 
Daniel Roy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all; thanks for taking the time to read and respond to my question.

In the Head First Java book, 2nd ed. (great job BTW Kathy and Bert), on page 576, there's a "Be the compiler, advanced" exercise that I can't seem to figure out, and the answers in the book are different than what Sun's 1.5.0_07 javac is giving me. The exercise shows a list of sample declarations using generics; I wrote some code around them and ran it through a compiler. Here's my code:

import java.util.*;

public class TestGenerics4 {

public static void main(String[] args) {
new TestGenerics4().go();
} // method main

public void go() {
ArrayList<Dog> dogs1 = new ArrayList<Animal>(); // 1
ArrayList<Animal> animals1 = new ArrayList<Dog>(); // 2
List<Animal> list = new ArrayList<Animal>(); // 3
ArrayList<Dog> dogs = new ArrayList<Dog>(); // 4
ArrayList<Animal> animals = dogs; // 5
List<Dog> dogList = dogs; // 6
ArrayList<Object> objects = new ArrayList<Object>(); // 7
List<Object> objList = objects; // 8
ArrayList<Object> objs = new ArrayList<Dog>(); // 9
} // method go

public void takeAnimals(ArrayList<Animal> animals) {
for(Animal a: animals) {
a.eat();
} // for
} // method takeAnimals
} // class TestGenerics4

abstract class Animal {
void eat() {
System.out.println("animal eating");
} // method eat
} // class Animal

class Dog extends Animal {
void bark() {}
} // class Dog

class Cat extends Animal {
void meow() {}
void eat() {
System.out.println("cat eating");
} // overriden method eat
} // class Cat

javac returns the following:
E:\Programming\Java\Head First Java\Ch16>javac TestGenerics4.java
TestGenerics4.java:10: incompatible types
found : java.util.ArrayList<Animal>
required: java.util.ArrayList<Dog>
ArrayList<Dog> dogs1 = new ArrayList<Animal>(); // 1
^
TestGenerics4.java:11: incompatible types
found : java.util.ArrayList<Dog>
required: java.util.ArrayList<Animal>
ArrayList<Animal> animals1 = new ArrayList<Dog>(); // 2
^
TestGenerics4.java:14: incompatible types
found : java.util.ArrayList<Dog>
required: java.util.ArrayList<Animal>
ArrayList<Animal> animals = dogs; // 5
^
TestGenerics4.java:18: incompatible types
found : java.util.ArrayList<Dog>
required: java.util.ArrayList<java.lang.Object>
ArrayList<Object> objs = new ArrayList<Dog>(); // 9
^
4 errors

Note that the book suggests that lines 3, 6, 7, and 8 should not compile. Is there simply an error in the book?

Kind regards,
Daniel
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at the question in the book, and it looks like they intended the X to mark the ones that would compile.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else is having the same problem. See this thread.

CR
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the other thread I quoted earlier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!