Junior
How to determine that a class is a subclass of another class?
sobes.tech AI
Answer from AI
Using the instanceof operator or the isInstance() method of the Class class.
// Class A
class A {
//...
}
// Class B, inheriting from A
class B extends A {
//...
}
// Creating instances
A instanceA = new A();
B instanceB = new B();
// Check using instanceof
boolean isBInstanceOfA = instanceB instanceof A; // true
boolean isAInstanceOfB = instanceA instanceof B; // false
// Check using isInstance()
boolean isBInstanceOfAByClass = A.class.isInstance(instanceB); // true
boolean isAInstanceOfBByClass = B.class.isInstance(instanceA); // false