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

Binding TextColor of Label inside StackLayout with BindingContext

c# xaml binding xamarin.forms staticresource

I need to Bind TextColor of Label. But the Label is inside a StackLayout with BindingContext of SelectedArticle, hence the Binding is not working with anything outside SelectedArticle for Binding (...

possible prefixes for bundle.getLocation()

java eclipse eclipse-plugin osgi

I'm using the Equinox OSGi implementation. When I retrieve a bundle and ask it for its location (getLocation() method) the returned String is prefixed with "initial@reference". The OSGi 4.3 specifi...