Skip to content

Files

Latest commit

 

History

History
39 lines (26 loc) · 933 Bytes

EqualsWithHashCodeExist.md

File metadata and controls

39 lines (26 loc) · 933 Bytes

Pattern: Missing override for equals() or hashCode()

Issue: -

Description

When a class overrides the equals() method it should also override the hashCode() method.

All hash-based collections depend on objects meeting the equals-contract. Two equal objects must produce the same hashcode. When inheriting equals or hashcode, override the inherited and call the super method for clarification.

Example of incorrect code:

class Foo {

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }
}

Example of correct code:

class Foo {

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }

    override fun hashCode(): Int {
        return super.hashCode()
    }
}

Further Reading