How to Override equals() Method in Java?

We override equals() method in Java to check if two objects are equal. Before overriding equals() method in Java, first let's see when two objects are considered to be equal. Two objects are considered to be equal when they are identical (contain the same data) or in other words they are in the same state. In order to compare two objects for equality, we need to override equals() method because it is originally defined in Object class that takes a parameter of type Object and compares it with this reference. It does not make member level comparison. Following is the piece of code of equals() method from Object class.

public boolean equals(Object obj) 
{
  return (this == obj);
}

The above implementation of equals() will only return true if two references point to the same object in memory because it compares memory locations with == operator rather than comparing contents. If two objects having same data are stored at different locations in memory then above implementation will return false. So, when we define our own data types (classes), we need to override equals(). Java's convention is that equals() must be an equivalence relation. Therefore, overridden equals() method must have the following properties:

  1. Reflexive: x.equals(x) is true.
  2. Symmetric: x.equals(y) is true if and only if y.equals(x).
  3. Transitive: if x.equals(y) and y.equals(z) are true, then so is x.equals(z).

In addition to the above rules, equals() must take an Object as argument and satisfy the following properties.

  1. Consistent: multiple invocations of x.equals(y) consistently return the same value, provided neither object is modified.
  2. Not null: x.equals(null) returns false.

By following above rules, let's implement equals() method for the class Person as follows:

class Person 
{
  private String fname;
  private String lname;
  private int age;
 
  public Person(String fname, String lname, int age)
  {
    this.fname = fname;
    this.lname = lname;
    this.age = age;
  }
 
  //Overriding equals
  public boolean equals(Object obj) 
  {
    if (this == obj) return true;
    if (obj == null) return false;
    if (this.getClass() != obj.getClass()) return false;
    Person that = (Person) obj;
    if (this.age != that.age) return false;
    if (!this.fname.equals(that.fname)) return false;
    if (!this.lname.equals(that.lname)) return false;
	return true;
  }
}
 
public class EqualsDemo
{
  public static void main(String str[])
  {
    Person p1 = new Person("Anushka", "Krishan", 7);
    Person p2 = new Person("Anushka", "Krishan", 7);
    System.out.println(p1.equals(p2));
    System.out.println(p1.equals(p1));
    System.out.println(p2.equals(p1));
  }
}
 
OUTPUT
======
D:\JavaPrograms>javac EqualsDemo.java
D:\JavaPrograms>java EqualsDemo
true
true
true
 

In above piece of code class Person has an overridden equals() method, which took the following step-by-step approach:

  1. If the reference to this object is the same as the reference to the argument object, return true. This test saves the work of doing all the other checks in this case.
  2. If the argument is null, return false.
  3. If the objects are not from the same class, return false. To determine an object's class, we use getClass(). Note that we can use == to tell us whether two objects of type Class are equal because getClass() is guaranteed to return the same reference for all objects in any given class.
  4. Cast the argument from Object to Person (this cast must succeed because of the previous test).
  5. Return false if any instance variables do not match.

Once you have overridden equals(), you have made your class comparable.

Hope you have enjoyed reading how to override equals method in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.