ArrayList and elements

first off…Hi! first time posting.

ok, ive been using java for a little over half a year now and the APIs, google, and trial and error have solved all my problems up until now.
ive learned that u can set an ArrayList’s type by using the and the object name. my question is two part.

ive got a parameter that can take a superclass(or basically a designed class to help with interfacing) and i want the method to get the actual sub class.

secondly i want to to create an arraylist fixed with the obtained subclass if possible.

iv tried googling it and no real help there. and the api refers u back to arraylist when u click on element. ive tried using the object method getClass(), but that wont go into the area and work with the compiler. Anyone able to help me with any of those things? it would be much appreciated.

I suspect that you are falling foul of a design feature in the JVM which is ‘type erasure’. The JVM does not understand generic types; an ArrayList is just an ArrayList to the JVM. getClass() therefore just returns ArrayList. Generics are only used for compile-time checking, not runtime type safety.

Cas :slight_smile:

IMHO, that sounds like you’ve got a problem in your design somewhere…