Listing 2: An implementation of a mixed-type comparison for a subclass in a class hierarchy

public class SubClass extends RootClass {
    static final int
        b1Default = 0,
        b2Default = 0;
    private int b1 = b1Default;
    private int b2 = b2Default;
    
    protected boolean _navigateClassHierarchy(Object other, 
                                              boolean reverseOrder) {
        if (other instanceof SubClass && !reverseOrder) 
        {  // reverse order         
           return 
              ((SubClass)other)._navigateClassHierarchy(this,true);
        }
        else 
        {   // compare my fields 
            if(!_compareFields(other)) return false;
            // delegate to super
            return super._navigateClassHierarchy(other,reverseOrder);
        }
    }

    private boolean _compareFields(Object other) {
        if (other instanceof SubClass) {    // at least my type, 
                                            // check fields
            SubClass myType = (SubClass)other;
            if (b1 != myType.b1
                || b2 != myType.b2) return false;
        } else {                            // check defaults
            if (b1 != b1Default
                || b2 != b2Default) return false;
        }
        return true;
    }
}


— End of Listing —