Testing linear class relationship

I'm calling this linear class relationship, but correct me if I'm wrong. I'm looking to test whether or not the class of object A is an ancestor or descendant of object B's class.

For example, AbstractCollection is linearly related to both Object and ArrayList. However, ArrayList is not linearly related to Vector.

My first stab was:

//...Assume objects A and B were declared...

Class<? extends Object> Aclass = A.getClass();
if(Aclass.isAssignableFrom(B.getClass()) || Aclass.isInstance(B)){
    //Their types are linearly related, at the least
}

Is this an appropriate check?

Answers


Your test works. I prefer the symmetric check:

if (a.getClass().isInstance(b) || b.getClass().isInstance(a)) { 
}

Need Your Help

No value is present in hidden field after page load

javascript asp.net

In the below code i have coded in page load.In this i have store regular expressions in hidRegExp.Value. Now i have to store this value in the hidden field.Now in javascript i have to validate the...

Google Form Submit > Attach New Responses to Email and Send

google-apps-script google-form

I have a script that turns a submitted form into a PDF and emails it automatically but I can't seem to get my head around if it is possible to implement a function i would like: -