21

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?

Community
  • 1
  • 1
MBZ
  • 26,084
  • 47
  • 114
  • 191

3 Answers3

34

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:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
16

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
3

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.

Ahmad
  • 2,110
  • 5
  • 26
  • 36
  • +1, nice answer. Regarding your last point though, there are a few gotchas. If an employee is *mutable* the approach of checking some employee id may be risky. – aioobe Jun 13 '12 at 09:59
  • Well...if something is mutable..they you are totally out of control. The whole equals() world fails. – Ahmad Jun 13 '12 at 10:06
  • what's your reasoning behind that? – aioobe Jun 13 '12 at 11:00
  • well...you fear that if it is mutable then Employee ID will not be consistent..then ..whatelse could be consistent to ensure equality.. – Ahmad Jun 13 '12 at 11:07