In Java class can inherit from another class. So the ‘child’ class have its parent. How to check if one class is a subclass of another class in Java ?
One of the solutions is to use isAssignableFrom method that returns boolean value (True of False). You have to call this method on the parent class and in parentheses pass the child class to check if caller class is somehow ancestor of ‘child’ class . (I know … loads of ‘parent’ and ‘child’ words here)
example usage :
Let the class : MyException exteds RuntimeException
How to check if we are not sure if MyException inherits form RuntimeException ? (In this example we know, but if we have entire cascade of inheritance we may not be so sure about all inheritances)
Java code to check if one class is a parent of another class :
boolean isRelated = RuntimeException.class.isAssignableFrom(MyException.class) ;