when should I override Equals function?

Possible Duplicate:
Java: Always override equals?

should I override equals function for any class that I create?

even for very simple classes which only contains some very simple attributes and by equals I need every single attribute of it to be equal?

Why should I override hashCode() when I override equals() method?

Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; pub

Why overriding == must override equals? [duplicate]

Possible Duplicate: Is it necessary to override == and != operators when overriding the Equals method? (.NET) C# compiler prompts me that I should override equals if overriding ==, I just want to kn

Apart from immutable value objects, when should I override `equals()`?

It’s clear that equals() (and of course hashCode()) are valuable when you’re dealing with immutable value objects — map keys, strongly typed field values that you need to compare across the objects t

When do I need to override equals and hashcode methods? [duplicate]

Possible Duplicate: Overriding equals and hashCode in Java If I have class A { int x = 1; } … A a1 = new A(); A a2 = new A(); a1.equals(a2); If I compare 2 instances of A without override the equ

For contains should we override both hashcode and equals for hashset in java [duplicate]

Possible Duplicate: Overriding equals and hashCode in Java For contains should we override both hashcode and equals for hashset in java? import java.lang.Math; import java.util.HashSet; class Hello{

What can go wrong if one fails to override GetHashCode() when overriding Equals()? [duplicate]

Possible Duplicate: Why is it important to override GetHashCode when Equals method is overridden? In C#, what specifically can go wrong if one fails to override GetHashCode() when overriding Equals(

Should I specify ‘virtual’ again when I override a C++ function?

When I override a virtual function: class Geoff { public: virtual int getArea() { return 0; } } Should I specify ‘virtual’ again when I override it? Does it make any difference? I know both ways seem

Overriding hashCode() when overriding equals() [duplicate]

Possible Duplicate: In Java, why must equals() and hashCode() be consistent? I read that one should always hascode() when overriding equals(). Can anyone give a practical example of why it might be

Why do I need to override the .Equals and GetHashCode in C# [duplicate]

This question already has an answer here: Why is it important to override GetHashCode when Equals method is overridden? 11 answers I am using Entity Framework 5. In my C# code I want to compare

Should mutable collections override equals and hashCode?

I was just wondering if it was a good idea to override equals and hashCode for mutable collections. This would imply that if I insert such a collection into a HashSet and then modify the collection, t

Answers

should I override equals function for any class that I create?

Override equals if (and only if) the object “represents some data”, i.e. if it models something such as Person, Car or RecipieIngredient (these typically end up in collections etc). Don’t override equals for other types of classes, for example LoginServlet or DatabaseUtil.

Remember to always override hashCode whenever you override equals.

(A natural follow-up question:) What happens if I don’t override equals and hashCode?

Any two objects will be considered unequal unless they are the exact same object.

[…] I need every single attribute of it to be equal?

Typically yes. It depends on how you define your notion of equality. Note that for reference types, you can reuse/delegate to that objects implementation of equals (and hashCode) when implementing your own.

Related questions:

You should only override equals() if you have a reason to do so. As described here, it is very difficult to write a proper equals() method for non-final or mutable classes.

If your application requires some sort of equality concept that is different from “the identical object”, then by all means go ahead. Just read the above reference to be aware of what’s involved. But as a matter of routine? Definitely not.

Well, the concept of over-riding equals() is more understood if you think it in real-life terms.

Equals method should be over-ridden only when two objects need to be equal logically. Also, if you fear, somewhere in your programme an object may be re-created, then you must over-ride equals().

A very good example is String objects in java.

   String string1= new String("hello");

   String string2= "hello";

Are they equal?.. yes absolutely they are..logically though. You are able to check their equality only because, Java has already over-ridden String equals() method.

As we all know, a class is actually a template for its objects. So let’s consider, there is an employee class, which actually determines what attributes employees in the company may hold and actual employees in the company are objects of this class. So typically, attributes of an employee may be as follows.

1.Employee Name

2.Employee ID

3.Date Of Birth . . . .. . . . . . .

So in this case , you should just check whether Employee ID is equal or not in your equals method. But yeah, if your objects lacks such distinct attribute, then you should go ahead and check almost all values to avoid make your programme consider two diffrent people equal.