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.
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