ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to Override equals method in Java ?.

Updated on September 17, 2014

Hello friends,

In this hub, we are going to discuss, how to override equals method in Java. The equals method is implemented in Object class. The equals method in object class has signature as : public boolean equals(Object obj), taking in object type as parameter.

This method in object class returns true, if object calling equals method is referring to same object which is coming as parameter to equals method. Whenever you want to compare whether two objects have the same contents, equals method is called.

Here, in slide there are two employee objects, such as employee1 and employee2.

Employee class has two properties as : age and name.

public class Employee {

	private int age;
	private String name;

	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Employee.java (without equals method)

Lets discuss code of employee class point by point.

Number 1 - Lets create a class Employee.

Number 2 - In the employee class, create two instance variables as , age and name.

Number 3 - Provide getters and setters for each of the property.

This class is without equals method overridden.

It has default functionality for the equals method, which has been provided to it by Object class.

public class EqualsTest {

	public static void main(String[] args) {

		Employee employee1 = new Employee();
		employee1.setAge(27);
		employee1.setName("Dinesh");

		Employee employee2 = new Employee();
		employee2.setAge(27);
		employee2.setName("Dinesh");

		if(employee1.equals(employee2)) {
			System.out.println("Contents of employee1 and employee2 are equal");
		}
		else {
			System.out.println("Contents of employee1 and employee2 are not equal");
		}

	}

}

EqualsTest.java

Lets create a test class by name Equals Test, and discuss its functionality point by point.

Number 1 - Create a main method. In the main method, create two employees by name , employee1 and employee2. Set name and age for both employees to "Dinesh" and 27.

Number 2- In order to check whether the contents of the two object are equal, we call employee1.equals(employee2).

As per our scenario and definition, it must return true and it should print something as ,

Contents of employee1 and employee2 are equal.

But when we run, equals test class, we get output as,

Contents of employee1 and employee2 are not equal, as shown in figure below.

Employee.java (with equals method)

In order to resolve above problem, lets override equals method in Java.



public class Employee {

	private int age;
	private String name;

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;

		if (obj == null) return false;

		if (this.getClass() != obj.getClass()) return false;

		Employee other = (Employee) obj;

		if (this.age != other.age)
			return false;

		if (this.name == null) {
			if (other.name != null)
				return false;
		} 
		else if (!this.name.equals(other.name))
			return false;

		return true;
	}


	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}	 

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article