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

javascript - CasperJS/PhantomJS failing SSL handshakes on some sites even with --ssl-protocol=any -

codeigniter - Fatal error: Call to undefined function lang() in CI Merchant using CardSave -

python - Received unregistered task using Celery with Django -