I'd like to check if a Class object represents a subclass of other class for example
Class class1 = Class.forName("Class1");
Class class2 = Class.forName("Class2");
if(class1.isSubClassOf(class2)) // fake methos isSubClassOf
{
// do sth
}
How can I implement this isSubClassOf
method ?
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
The isinstance() method checks whether an object is an instance of a class whereas issubclass() method asks whether one class is a subclass of another class (or other classes).
Python issubclass() This function is used to check whether a class is a subclass of another class.
Can an object be a subclass of another object? A. Yes—as long as single inheritance is followed.
Class.isAssignableFrom() provides more-or-less what you're after, although it handle interfaces also, so may need to do a bit more extra work to be sure that it's a subclass, direct or otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With