aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics 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 "Generics" Watch "Generics" New topic
Author

Generics

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

What is the difference between lin1 and line 2.
Line 1 creates a error whereas line 2 does not.
As far i understood generic type of argument and parameter should be same thats why line one error is generated.
What is happening at line 2 ? ?




SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
doc.checkAnimals((ArrayList)cats);

here you are doing
ArrayList<Animal> animals=new ArrayList();

as you are converting cats to raw type; That is only generate unchecked warning.

In first case you are doing
ArrayList<Animal> animals=new ArrayList<Dog>();


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

James if you have copied this question from somewhere then please Quote Your Sources...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Now i got it...
And
if i change this line
doc.checkAnimals((ArrayList)cats);
to
doc.checkAnimals((ArrayList < Animal > ) cats);
then it says its inconvertable types. what does this message mean??

And this code is a modified code of K&B book.
[ January 02, 2009: Message edited by: James Tharakan ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
doc.checkAnimals((ArrayList)cats); means

ArrayList temp=cats; that is actually
ArrayList temp=new ArrayList<Cat>();



doc.checkAnimals((ArrayList < Animal > ) cats); means
ArrayList<Animal> temp=cats; that is actually
ArrayList<Animal> temp=new ArrayList<Cat>();
then it says its inconvertable types.
Inconvertable means you cannot convert Cat list to Animal list.
[ January 02, 2009: Message edited by: Punit Singh ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

ArrayList temp=cats; that is actually
ArrayList temp=new ArrayList<Cat>();

Are you sure that it creates a new ArrayList ? ?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
No James

public static void main(String[] args)
{
AnimalDoctorGeneric doc = new AnimalDoctorGeneric();

ArrayList<Dog> dogs = new ArrayList<Dog>();
ArrayList<Cat> cats = new ArrayList<Cat>();
Actually I am showing this line, that is referred by cats reference.


doc.checkAnimals( dogs); // Line 1
doc.checkAnimals((ArrayList)cats); // Line 2

}
[ January 02, 2009: Message edited by: Punit Singh ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

ok,
but punit, why is it inconvertable? I mean cat is a subclass of animal right.
Animal list can have cats and dogs right??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Animal list can contain Cat and Dog, but here it is an opposite case. You are converting a whole list of Cats into list of animals. Then you will be able to add dogs into the list. Look at this

ArrayList<Animal> animal = new ArrayList<Cat>(); //if this was allowed then following will create problems
animal.add(new Dog()); //Oops we added a Dog into a Cat List
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Ok, now i understood the point.
Thanks guys
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
ArrayList<Animal> animal = new ArrayList<Cat>(); //if this was allowed then following will create problems
animal.add(new Dog()); //Oops we added a Dog into a Cat List


In addition to this
if
ArrayList<Animal> animal = new ArrayList<Animal>();
then
animal.add(new Dog());
animal.add(new Cat()); allowed

Why
ArrayList<Animal> animal = new ArrayList<Cat>(); not allowed ?
As JVM has not runtime information about type of ArrayList, JVM will only knows that it is just an ArrayList means jvm will see this code:

ArrayList animal=new ArrayList();

and this code can take any object. So to avoid above condition mentioned by Ankit, compiler only allows type-safe conversions, such that ArrayList<Cat> can not contain new Dog();. Think in terms of compiler, you will grasp it easily. Again read K&B book for this topic and do some meditation over this.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Again read K&B book for this topic and do some meditation over this.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Well I was trying to avoid telling this as it might be confusing but it is required now.

If you add a Dog to the Cat list, it will not be a problem and you can do this using legacy code



When you added a dog to the cat list, then there is no exception as the list is actually a raw list for the JVM. But then you will get an exception when you get the element from the Cat list as Dog is not a Cat...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Its okay ankit nothing is confusing for me now.Actually i almost finished reading the chapter of generics.It is just the casting that created a doubt.
Now everything is clear(hopefully ).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics