Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

What is the point of overriding the equals method?

I am currently doing an online course for Java. One of the assignments involves writing a Team class. I have no problem with that. The last part of the assignment, it says

"Add an override of the equals method".

What is the point of overriding this? I understand what the method does and what it is used for.
The point is for you to easily specify how to differentiate between two objects of same class. Imagine two objects of type Dog. If one Dog has the same name and race as another Dog, are the two Dog's 100% identical? No. Alright, so we need to add more information. This is where you could add an owner, parents and ear marking or whatever. If all of those fields are the same, then yeah, you could say the two objects are the same (or that the two references refer to the same object).

Essentially, you want to override it to be able to specify what equality is of your class. Integer objects could be identical if they have the same value, even if they are two independent objects (located at two different addresses in memory).

Also: always override hashCode when overriding equals. This is best practice, at least. You can usually have your IDE generate those methods for you - at least this will give you an idea of what the methods could look like.
[Image: TEGE4Zx.png]
Puzzle level: 2/5
(Sep 27, 2014 12:48 PM)Nov Wrote:  I am currently doing an online course for Java. One of the assignments involves writing a Team class. I have no problem with that. The last part of the assignment, it says

"Add an override of the equals method".

What is the point of overriding this? I understand what the method does and what it is used for.

I've never read about it but usually it checks the reference.

Now for example if you have 2 different instances of teams but they're technically both the same(Same amount of players, same players etc...) you could override the equals method to check if they have the same content.

Again, I've never read/done much with the equals method so there probably are other reasons for overriding it.
(Sep 27, 2014 12:57 PM)MrOrang Wrote:  I've never read about it but usually it checks the reference.

Now for example if you have 2 different instances of teams but they're technically both the same(Same amount of players, same players etc...) you could override the equals method to check if they have the same content.

Again, I've never read/done much with the equals method so there probably are other reasons for overriding it.

That is the reason. If you have two football teams with the exact same players, you could define that those two teams are in fact equal, even if they appear two places in memory and even if changing one team doesn't affect the other (team1.kickPlayer("Foo") doesn't kick "Foo" from team2).
[Image: TEGE4Zx.png]
Puzzle level: 2/5
I understand. Thanks for the explanations.
(Sep 27, 2014 01:02 PM)Nov Wrote:  I understand. Thanks for the explanations.

Any time, man. Have a nice weekend 🤝
[Image: TEGE4Zx.png]
Puzzle level: 2/5


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FinnTheHumanJul 9, 2017 09:31 PM 1 181 Jul 9, 2017 09:31 PM
Last Post: Rageline
[Java] by Zippy™Apr 15, 2017 09:40 AM 7 289 Apr 15, 2017 09:40 AM
Last Post: Zippy™
[Java] by Stinger910Oct 6, 2015 04:58 PM 4 160 Oct 6, 2015 04:58 PM
Last Post: Natecat33