File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method." Watch "Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method." New topic
Author

Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method.

Suresh Rachuru
Greenhorn

Joined: Nov 26, 2011
Posts: 3
Hi,

In the below class, the method [quote] declaration public void checkAnimals(ArrayList<Animal> animals)[/quote] should have been more accurately as

[quote]public void checkAnimals(List<Animal> animals) [/quote]

OR

List<Dog> dogs = new ArrayList<Dog>(); should change to ArrayList<Dog> dogs = new ArrayList<Dog>() respectively for other two List variable declarations. Any comments please ? Thanks!

public class AnimalDoctorGeneric {
// change the argument from Animal[] to ArrayList<Animal>
public void checkAnimals(ArrayList<Animal> animals) {
for(Animal a : animals) {
a.checkup();
}
}
public static void main(String[] args) {
// make ArrayLists instead of arrays for Dog, Cat, Bird
List<Dog> dogs = new ArrayList<Dog>();
dogs.add(new Dog());
dogs.add(new Dog());
List<Cat> cats = new ArrayList<Cat>();
cats.add(new Cat());
cats.add(new Cat());
List<Bird> birds = new ArrayList<Bird>();
birds.add(new Bird());
// this code is the same as the Array version
AnimalDoctorGeneric doc = new AnimalDoctorGeneric();
// this worked when we used arrays instead of ArrayLists
doc.checkAnimals(dogs); // send a List<Dog>
doc.checkAnimals(cats); // send a List<Cat>
doc.checkAnimals(birds); // send a List<Bird>
}
}
Tse Wu
Greenhorn

Joined: Jan 05, 2012
Posts: 10
Hi,
I think it should be the following

public void checkAnimals(List<? extends Animal> animals)

Since passing List is a type of <Dog>, <Cat> or <Bird> you cannot pass the <Dog> type into an <Animal> type directly other than extending it with ?.


OCPJP 1.6
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
Tse Wu wrote:

public void checkAnimals(List<? extends Animal> animals)

Since passing List is a type of <Dog>, <Cat> or <Bird> you cannot pass the <Dog> type into an <Animal> type directly other than extending it with ?.



True. That way the compiler can prevent you from adding a Cat to a List<Dog> in the checkAnimals method (animals.add(new Cat);//when animals collection passes is of type List<Dog>) as Cat is also a type of Animal!
Courtesy Kathy Sierra Book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chapter 7 - Objective 6.3 & 6.4 - type parameter for method checkAnimals () method.