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 -

tags - Jquery Mixitup plugin help prevent handlers being destroyed -

c# - Delving into the world of XML (Windows Phone) Error I dont understand (The ' ' character, hexadecimal value 0x20, cannot be included in a name.) -