Overriding equash method of HashMap in Java

I noticed that in the source code for the HashMap, it lists the equals method as final. Why does this happen when I override it I don't get a compile error?

public class Test extends HashMap<Object, Object> {

    @Override
    public boolean equals(Object o) {
        return false;
    }
}

      

Java HashMap method is:

public final boolean equals(Object o) {
    if (!(o instanceof Map.Entry))
        return false;
    Map.Entry e = (Map.Entry)o;
    Object k1 = getKey();
    Object k2 = e.getKey();
    if (k1 == k2 || (k1 != null && k1.equals(k2))) {
        Object v1 = getValue();
        Object v2 = e.getValue();
        if (v1 == v2 || (v1 != null && v1.equals(v2)))
            return true;
    }
    return false;
}

      

+2


source to share


1 answer


To make the equals method for HashMap.Entry

rather than HashMap

- look how it tries to use the reference passed into it as Map.Entry

.



+13


source







All Articles