In the Android app test suite, I have a class like this, where B is the view:
public class A extends B { ... etc... }
I now have a list of view objects that can contain objects of A , but in this case I don’t care if they are subclasses or “instances” of B I would like to do something like:
ArrayList<View> viewList = getViews(); Iterator<View> iterator = viewList.iterator(); while (iterator.hasNext() && viewList != null) { View view = iterator.next(); if (view.getClass().isInstance(B.class)) { // this is an instance of B } }
The problem is that when if encounters object A , it does not evaluate "instance B ". Is there a way to do isSubclassOf or something else?
java instanceof subclass
iamamused Mar 09 '10 at 15:46 2010-03-09 15:46
source share