Search code examples
javaclasscomparisonsuperclass

Smart way to check super-class


public  boolean isUserControled(){      
        return action.getClass().getSuperclass().toString().equals("class logic.UserBehaviour");
}

I think this piece of code is pretty self-explanatory. Is there a smarter way to do this?

Thanks


Solution

  • Unless you specifically want to check only the very first superclass, it would be better to use:

    return (action instanceof logic.UserBehavior);
    

    Your method would be better with this:

    action.getClass().getSuperClass().name().equals("logic.UserBehavior");
    

    The call to toString() is not the best idea.

    Or better yet, as posted by Ulrik:

    action.getClass().getSuperClass() == logic.UserBehavior.class