With a hierarchy of equals() methods, I prefer using an internal value check
for the parent values, and add in a class-specific check in the public equals()
method.

For example:

public class Parameter...

    public boolean equals(Object obj)
    {
        boolean result = equalsInternal(obj) 
                         && this.getClass().equals(obj.getClass());
        
                return result;
    }

    protected boolean equalsInternal(Object obj)
    {
        boolean result = (this == obj);
        
        if (!result && obj instanceof Parameter)
        {
            Parameter other = (Parameter) obj;
            result = ((this.name == null 
                            ? other.name == null 
                                    : this.name.equals(other.name))
                    && this.value == null 
                            ? other.value == null 
                                    : this.name.equals(other.value));
        }
        
        return result;
    }



public class Cookie...

    public boolean equals(Object obj)
    {
        boolean result = equalsInternal(obj) 
                         && this.getClass().equals(obj.getClass());
        
        return result;
    }
    
    protected boolean equalsInternal(Object obj)
    {
        boolean result = (this == obj);
        
        if (!result && obj instanceof Cookie)
        {
            Cookie other = (Cookie) obj;
            result = super.equalsInternal(other) // check parent's equality
                     && (this.domain == null 
                                 ? other.domain == null 
                                         : this.domain.equals(other.domain))
                     && (this.path == null 
                                 ? other.path == null 
                                         : this.path.equals(other.path));
        }
        
        return result;
    }

Reply via email to