As we know that we can use instanceof operator to check whether object A is an instance of class A. Similarly, I was wondering whether is there anyway we can check whether object A is an instance of class B, where both, class A and class B extends X. ie Classes A and B are siblings
As we know that we can use instanceof operator to check whether object A
Share
Maybe I misunderstood you, but I am assuming here that you don’t know X, or that you want to find the answer for any X.
Well, all classes are siblings, as in, they all have
Objectas a common ancestor, so your method would always return true.What you might want to do is check out what is the closest ancestor for two classes, which you could do using reflection to get all parent classes, as in:
And compare them all (post if you have problems with the code!)