Java - How Can I Check If A Class Is Inheriting From Some Class Or Interface?

instances, java, reflection

Solution

Use `isAssignableFrom`

if(d.isAssignableFrom(c)){
    // then d is a superclass of c
    // in other words, c inherits d
}

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. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Source

Problem

I need to check: ``` public static boolean check(Class<?> c, Class<?> d) { if (/* c inherits from d */) return true; else return false; } ``` How can I do that ? And is that possible without `c.newInstance()` ? The title was wrong at the first time. Now it's correct.

Original source