How to test if one java class extends another at runtime?
问题 How to I test if a is a subclass of b ? Class<?> a = A.class; Class<?> b = B.class; 回答1: Are you looking for: Super.class.isAssignableFrom(Sub.class) 回答2: If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class). For your example, it would be: if(B.class.isAssignableFrom(A.class)) { ... } If you're interested in whether or not an instance is of a particular type, use instanceof : A obj = new A(); if(obj instanceof B) { ... } Note that these will return