How do I determine if a class extends another class in Java?

The getSuperClass() approach would fail for E since its immediate superclass is not A, but B. Rather use Class#isAssignableFrom().

public void myFunc(Class cls){
     //need to check that cls is a class which extends A
     //i.e. B, C and E but not A or D

     if (cls != A.class && A.class.isAssignableFrom(cls)) {
         // ...

Leave a Comment