Why you should care
“equals” as a method name should be used exclusively to override Object.equals(Object) to prevent any confusion. It is tempting to overload the method to take a specific class instead of Object as parameter, to save the class comparison check. However, this will not work as expected when that is the only override.
In addition, issues with Overloaded Equals:
- All the Collections provided by Java ie; Set, List, Map use the overridden method for comparing two objects.
So even if you overload the equals method, it doesn’t solve the purpose of comparing two objects.
Also, if you just overload and implement the hashcode method, it would result in erroneous behavior - If you have both overloaded and overridden equals methods and exposing both these methods you are going to confuse the client side developers.
It is by convention people believe that you are overriding the Object class
How we detect
CAST Highlight counts one occurrence each time an equals method is overloaded, i.e. has parameters with type other than Object.
Bad Code
class MyClass { private int foo = 1; public boolean equals(MyClass o) { // Noncompliant; does not override Object.equals(Object) return o != null && o.foo == this.foo; } public static void main(String[] args) { MyClass o1 = new MyClass(); Object o2 = new MyClass(); System.out.println(o1.equals(o2)); // Prints "false" because o2 an Object not a MyClass } } class MyClass2 { public boolean equals(MyClass2 o) { // Ignored; `boolean equals(Object)` also present //.. } public boolean equals(Object o) { //... } }
Good Code
class MyClass { private int foo = 1; @Override public boolean equals(Object o) { if (this == o) \{ return true; } if (o == null || getClass() != o.getClass()) { return false; } MyClass other = (MyClass)o; return this.foo == other.foo; } /* ... */ } class MyClass2 { public boolean equals(MyClass2 o) { //.. } public boolean equals(Object o) { //... } }
References
https://stackoverflow.com/questions/2910520/is-overloading-equals-worthwhile
https://rules.sonarsource.com/java/RSPEC-1210
https://rules.sonarsource.com/java/RSPEC-1210
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.