OVERRIDING equals() method :

String class, the wrapper classes and Collection have overridden the equals() method.
When you really need to know if two references are identical, use ==. But when
you need to know if the objects themselves (not the references) are equal, use the
equals() method. For each class you write, you must decide if it makes sense to consider two different instances equal. For some classes, you might decide that
two objects can never be equal. For example, imagine a class Car that has instance
variables for things like make, model, year, configuration—you certainly don't want
your car suddenly to be treated as the very same car as someone with a car that has
identical attributes. Your car is your car and you don't want your neighbor Billy
driving off in it just because, "hey, it's really the same car; the equals() method said so." So no two cars should ever be considered exactly equal. If two references refer to one car, then you know that both are talking about one car, not two cars that have the same attributes. So in the case of a Car you might not ever need, or want, to override the equals() method.

Class demo{
Int x=10;
Public static void main(String[] args){
Demo d1=new demo();
Demo d2=new demo();
System.out.println(d1.equals(d2));//It returns false
}}
Here internally two objects are equal. But it returns false.
So we have to override the equals method.
Consider the example below
class demo
{
int x;
String str;
demo(int x,String str){this.x=x;this.str=str;}
int getx(){return x;}
String getstr(){return str;}
public boolean equals(Object o){
if ((o instanceof demo)&&(((demo)o).getx()==this.x)&&(((demo)o).getstr()==this.str))
{return true;}
else {return false;}
}
public static void main(String[] args){
demo d1=new demo(5,"bhabani");
demo d2=new demo(5,"bhabani");
System.out.println(d1.equals(d2));
}};
now the output is true.
EXPLANATION:
First, be sure that the object being tested is of the correct type! It comes in
polymorphically as type Object, so you need to do an instanceof test on it. do the
instanceof test just to be sure that you could cast the object argument to the
correct type so that you can access its methods or variables in order to actually do
the comparison. if the object doesn't pass the instanceof test, then
you'll get a runtime ClassCastException.After that it checks the instance variables.
If all true then it returns true.
Equals() contract
The equals() Contract Pulled straight from the Java docs, the equals() contract says
■ It is reflexive. For any reference value x, x.equals(x) should return true.
■ It is symmetric. For any reference values x and y, x.equals(y) should
return true if and only if y.equals(x) returns true.
■ It is transitive. For any reference values x, y, and z, if x.equals(y) returns
true and y.equals(z) returns true, then x.equals(z) must return true.
■ It is consistent. For any reference values x and y, multiple invocations of
x.equals(y) consistently return true or consistently return false, provided
no information used in equals comparisons on the object is modified.
■ For any non-null reference value x, x.equals(null) should return false.

No comments:

Post a Comment