Override equals Method in Java
In Java, you can override the equals() method from the Object class to provide your own implementation for comparing the equality of objects. The equals() method is used to compare two objects for equality.
To override the equals() method, you need to follow these steps:
- Open the class for which you want to override the equals() method.
- Add the following method signature to your class:
@Override
public boolean equals(Object obj) {
// Implement your code here
}
The @Override annotation is optional but recommended, as it ensures that you are actually overriding a method from the superclass.
Example 1
In the above example, we have a Person class with name and age instance variables. We override the equals() method to compare two Person objects based on both the name and age values.
FileName: EqualsOverrideExample.java
class Person { private String name; // variable to store the name of the person private int age; // variable to store the age of the person public Person(String name, int age) { this.name = name; // Assigning the provided name to the name instance variable this.age = age; // Assigning the provided age to the age instance variable } @Override public boolean equals(Object obj) { if (this == obj) { return true; // Same object reference, so they are equal } if (obj == null || getClass() != obj.getClass()) { return false; // obj is null or belongs to a different class, so they are not equal } Person other = (Person) obj; // Casting obj to Person type return this.name.equals(other.name) && this.age == other.age; // Comparing name and age for equality } } class EqualsOverrideExample { public static void main(String[] args) { Person person1 = new Person("Ram", 32); Person person2 = new Person("Ram", 32); Person person3 = new Person("Sham", 11); System.out.println(person1.equals(person2)); System.out.println(person1.equals(person3)); } }
Output
true false
Example 2:
In the above program, we have created a Car class with brand, model, and year instance variables. We override the equals() method to compare two Car objects based on all three variables.
FileName: EqualsOverrideExample.java
class Car { private String brand; private String model; private int year; public Car(String brand, String model, int year) { this.brand = brand; this.model = model; this.year = year; } @Override public boolean equals(Object obj) { if (this == obj) { return true; // Same object reference, so they are equal } if (obj == null || getClass() != obj.getClass()) { return false; // obj is null or belongs to a different class, so they are not equal } Car other = (Car) obj; // Casting obj to Car type return this.brand.equals(other.brand) && this.model.equals(other.model) && this.year == other.year; // Comparing brand, model, and year for equality } } class EqualsOverrideExample { public static void main(String[] args) { Car car1 = new Car("Toyota", "Camry", 2021); Car car2 = new Car("Toyota", "Camry", 2021); Car car3 = new Car("Honda", "Civic", 2020); System.out.println(car1.equals(car2)); System.out.println(car1.equals(car3)); } }
Output
true false
Example 3
In this example, we have a Book class with title, author, and year instance variables. We override the equals() method to compare two Book objects based on all three variables. In the main() method, we create three Book objects: book1, book2, and book3. We then use the equals() method to compare book1 with book2 and book1 with book3. The program outputs true for the first comparison (since all variables are the same), and false for the second comparison (since the title and author are different).
FileName: EqualsOverrideExample.java
class Book { private String title; private String author; private int year; public Book(String title, String author, int year) { this.title = title; this.author = author; this.year = year; } @Override public boolean equals(Object obj) { if (this == obj) { return true; // Same object reference, so they are equal } if (obj == null || getClass() != obj.getClass()) { return false; // obj is null or belongs to a different class, so they are not equal } Book other = (Book) obj; // Casting obj to Book type return this.title.equals(other.title) && this.author.equals(other.author) && this.year == other.year; // Comparing title, author, and year for equality } } class EqualsOverrideExample { public static void main(String[] args) { Book book1 = new Book("Adiyogi", "Sadhguru", 2011); Book book2 = new Book("Adiyogi", "Sadhguru", 2011); Book book3 = new Book("Wings of Fire", "APJ abdul kalam", 1990); System.out.println(book1.equals(book3)); System.out.println(book1.equals(book2)); } }
Output
false true