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
Post a Comment