This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections and me... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections and me..." Watch "Collections and me..." New topic

Collections and me...

kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
Hi All,

pgm 1:

mport java.util.*;
abstract class Animal {
public abstract void checkup();
class Dog extends Animal {
public void checkup() { // implement Dog-specific code
System.out.println("Dog checkup");
class Cat extends Animal {
public void checkup() { // implement Cat-specific code
System.out.println("Cat checkup");
class Bird extends Animal {
public void checkup() { // implement Bird-specific code
System.out.println("Bird checkup");

public class AnimalDoctorGeneric2 {

public void checkAnimals(ArrayList<Animal> animals) //Line 1
for(Animal a : animals) {
public static void main(String[] args) {
// make ArrayLists instead of arrays for Dog, Cat, Bird
List<Animal> dogs = new ArrayList<Animal>(); //Line 2
dogs.add(new Dog());
dogs.add(new Dog());
List<Animal> cats = new ArrayList<Animal>();
cats.add(new Cat());
cats.add(new Cat());
List<Animal> birds = new ArrayList<Animal>();
birds.add(new Bird());
// this code is the same as the Array version
AnimalDoctorGeneric1 doc = new AnimalDoctorGeneric1();
// this worked when we used arrays instead of ArrayLists
doc.checkAnimals(dogs); // Line3

pgm 2:

import java.util.*;

class base2

static void chk(ArrayList<Byte> b)
public static void main(String[] s)
List<Byte> t=new ArrayList<Byte>();

I am using the same pgm1 Line1 ,Line2, Line 3 concepts in pgm2. pgm1 is compiles and runs.But pgm2 is not compiling and gives this error.List cannot be applied to ArrayList.But why pgm1 is compiling?Anyboby please clarify this.Advance Thanks.
wong sheena

Joined: Apr 07, 2007
Posts: 23
because of this line "static void chk(ArrayList<Byte> b)"

ArrayList implements List interface so you cannot pass a List reference variable to this method.

change to "static void chk(List<Byte> b)"
will be ok.
kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
Thanks for the reply.Pgm1 is working fine. I was using wrong class i.e AnimalDoctorGeneric1 instead of 2.
It is sorta covered in the JavaRanch Style Guide.
subject: Collections and me...
Similar Threads
Help me fix this-Part-2,so I can compile(generics doubt)
Doubt in K&B SCJP 5: topic Generics
Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method.
Help me fix this,so I can compile(generics doubt)