This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Generics exercise in Head First Java 2nd ed. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Generics exercise in Head First Java 2nd ed." Watch "Generics exercise in Head First Java 2nd ed." New topic
Author

Generics exercise in Head First Java 2nd ed.

Daniel Roy
Greenhorn

Joined: Mar 08, 2006
Posts: 1
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

Joined: Feb 07, 2005
Posts: 2341
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
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Somebody else is having the same problem. See this thread.

CR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Please see the other thread I quoted earlier.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generics exercise in Head First Java 2nd ed.
 
Similar Threads
Another question on Generics
Generics - return types
Generics...please clarify
Generics:Wild cards with upper Bounds
Doubt in K&B SCJP 5: topic Generics