| Author |
Generics
|
vibhas karn
Greenhorn
Joined: Aug 11, 2008
Posts: 26
|
|
Hi every body Its Vibhas once again. I am geting a doubt that why does this set of code is not working if i use ArrayList instead of List in the following code i have commented that part by >>>??? Question please have a look on that and expalin me with reason .. Here is the code [edit]Add code tags, and removed some excess whitespace. CR[/edit] [ September 19, 2008: Message edited by: Campbell Ritchie ]
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
vibhas karn: public void calldoctor(List<Animal> animal)//Instead of List if i use { //ArrayList its giving me //compiletime error
If you want this method's parameter to be ArrayList<Animal> then you should call that method passing ArrayList<Animal> to it. But you call that method from Reference Type is List. So you cannot assign List type in to collection originally declared as ArrayList, reverse is possible.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Thiago Sanchez
Greenhorn
Joined: Mar 28, 2008
Posts: 2
|
|
public class AnimalDoctorGenric2 { public static void main(String[] args) { List<Animal> mylist = new ArrayList<Animal>(); mylist is not an ArrayList, but a List. The JVM knows that an ArrayList is a List but not all List's are ArrayLists so you can do List<anyList> yourList = new ArrayList<anArrayList> but you can`t do ArrayList<anArrayList> youArrayList = new List<yourList>... so if you need your List to came an ArrayList again try that: ArrayList<Animal> anotherList; anotherList = (ArrayList)myList; Dog d = new Dog(); Cat c = new Cat(); Animal a = new Animal(); mylist.add(new Cat("Black")); mylist.add(new Dog("Doverman")); for(Object o : mylist) System.out.println(o); AnimalDoctorGenric2 ad = new AnimalDoctorGenric2(); //ad.calldoctor(d); or try it ad.calldoctor((ArrayList)mylist); //ad.calldoctor(c); } //>>>??? Question public void calldoctor(List<Animal> animal)//Instead of List if i use { //ArrayList its giving me //compiletime error //eventhough i am passing Animal //type reference....?? animal.add(new Dog("Germanshephered")); } } Sometimes programming means
|
 |
Thiago Sanchez
Greenhorn
Joined: Mar 28, 2008
Posts: 2
|
|
:shocked: I forget A DETAIL :shocked: ad.calldoctor((ArrayList<Animal>) mylist); //ad.calldoctor(c); } //>>>??? Question public void calldoctor(ArrayList<Animal> animal)//Instead of List if i use { //ArrayList its giving me //compiletime error //eventhough i am passing Animal //type reference....?? animal.add(new Dog("Germanshephered")); } } :shocked: [edit]Disable smilies. CR[/edit] [ September 19, 2008: Message edited by: Campbell Ritchie ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Your code is really difficult to read because you have too much whitespace. That might be caused by your text editor; are you using NotePad? If so, don't, but look for Notepad2 or Notepad++ which are much better tools for programming. They will also help with indentation, matching {} etc. I can't see what the problem is; I copied and pasted your code and it ran first time. What errors are you actually getting? The commented-out lines appear to call a method on the AnimalDoctor class which takes Dog or Animal as a parameter, but you have no such method. That might be the cause of your problem, which has nothing whatsoever to do with generics.
|
 |
vibhas karn
Greenhorn
Joined: Aug 11, 2008
Posts: 26
|
|
|
Sorry Ritche i got my mistake .... thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You're welcome ( ) but please tell us how you sorted it out.
|
 |
 |
|
|
subject: Generics
|
|
|