• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ? ?


 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>();
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James if you have copied this question from somewhere then please Quote Your Sources...
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList temp=cats; that is actually
ArrayList temp=new ArrayList<Cat>();

Are you sure that it creates a new ArrayList ? ?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, now i understood the point.
Thanks guys
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ).
 
reply
    Bookmark Topic Watch Topic
  • New Topic