How to check if a subclass is an instance of a class at runtime?

JavaSubclassInstanceof

Java Problem Overview


In an android app test suite I have a class like this where B is a view:

public class A extends B {
... etc...
}

now I have a list of view objects which may contain A objects but in this case I only care if they're subclasses or "instances of" B. I'd 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 the if encounters an A object it doesn't evaluate to an "instance of B". Is there a way to do isSubclassOf or something?

Java Solutions


Solution 1 - Java

You have to read the API carefully for this methods. Sometimes you can get confused very easily.

It is either:

if (B.class.isInstance(view))

API says: Determines if the specified Object (the parameter) is assignment-compatible with the object represented by this Class (The class object you are calling the method at)

or:

if (B.class.isAssignableFrom(view.getClass()))

API says: Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter

or (without reflection and the recommended one):

if (view instanceof B)

Solution 2 - Java

if(view instanceof B)

This will return true if view is an instance of B or the subclass A (or any subclass of B for that matter).

Solution 3 - Java

Maybe I'm missing something, but wouldn't this suffice:

if (view instanceof B) {
    // this view is an instance of B
}

Solution 4 - Java

Class.isAssignableFrom() - works for interfaces as well. If you don't want that, you'll have to call getSuperclass() and test until you reach Object.

Solution 5 - Java

It's the other way around: B.class.isInstance(view)

Solution 6 - Java

If there is polymorphism such as checking SQLRecoverableException vs SQLException, it can be done like that.

try {
    // sth may throw exception
    ....
} catch (Exception e) {
    if(SQLException.class.isAssignableFrom(e.getCause().getClass()))
    {
        // do sth
        System.out.println("SQLException occurs!");
    }
}

Simply say,

ChildClass child= new ChildClass();
if(ParentClass.class.isAssignableFrom(child.getClass()))
{
    // do sth
    ...
}

Solution 7 - Java

I've never actually used this, but try view.getClass().getGenericSuperclass()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniamamusedView Question on Stackoverflow
Solution 1 - JavaHardcodedView Answer on Stackoverflow
Solution 2 - JavaKrisView Answer on Stackoverflow
Solution 3 - JavaJoachim SauerView Answer on Stackoverflow
Solution 4 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 5 - JavanandaView Answer on Stackoverflow
Solution 6 - JavavientohoView Answer on Stackoverflow
Solution 7 - JavachamaView Answer on Stackoverflow