Java - InvocationTargetException when casting List to ArrayList -


say class defined like:

public class foodfactory{      protected elist<food> basket;      public list<food> getbasket(){         return basket;     } } 

the following throws exeption when try run it:

foodfactory factory = new foodfactory();  arraylist<food> foodbasket;  foodbasket = (arraylist<food>)getbasket(); 

and i'm not seeing why. did not cast properly?

well, long elist ot derived arraylist, there must classcastexception.

list<food> foodbasket = (list<food>) factory.getbasket(); 

don't use arraylist, use interface list.

by way, should use java naming conventions.

classnames start uppercase. valiables lowercase.


Comments

Popular posts from this blog

Line ending issue with Mercurial or Visual Studio -

java - Jtable duplicate Rows -

java - Run a .jar on Heroku -