Override equals method in Java

The object class's equals() method, which accepts an object as well as compares it with the current object, is used to compare two objects. If the references to these two objects were equal, the method returns true; otherwise, it does not.

Example

The class Employee throughout the following example has two variables: name and age, as well as a parameterized function Object() { [native code] }.

Using the equals() method, we create two objects from the main method by giving identical data and comparing the results.

This software returns false because the Object class' equals() method only returns true if the references to the objects are identical.

Example code for overriding the equals method:

EqualsExpl.java

Output:

Override equals method in Java

Overriding the equals() method

You can develop your own version of the equals method since the Object is indeed the superclass for all Classes in Java.

Example code:

EqualsExpl1.java

Output:

Override equals method in Java




Latest Courses