mercredi 12 juillet 2017

How to determine whether a Class is inherited of another?

I'd like to know if there is an easy and elegant way to determine whether a Class object has an inheritance relation to another.

All I found in the documentation is getSuperclass(), which gives the super class of a Class object. All I can come up with is this:

public static boolean isInherited(Class child, Class parent) {
    for (Class type = child; type != null; type = type.getSuperclass()) {
        if (type.equals(parent)) {
            return true;
        }
    }
    return false;
}

Am I missing out on something?





Aucun commentaire:

Enregistrer un commentaire