![]() |
|
[Java] What is the point of overriding the equals method? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] What is the point of overriding the equals method? (/showthread.php?tid=4464930) |
What is the point of overriding the equals method? - Nov - Sep 27, 2014 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. * - nOrr - Sep 27, 2014 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. * - MrOrang - Sep 27, 2014 (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 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. * - nOrr - Sep 27, 2014 (Sep 27, 2014 12:57 PM)lucamasira Wrote: I've never read about it but usually it checks the reference. 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). * - Nov - Sep 27, 2014 I understand. Thanks for the explanations. * - nOrr - Sep 27, 2014 (Sep 27, 2014 01:02 PM)Nov Wrote: I understand. Thanks for the explanations. Any time, man. Have a nice weekend 🤝 |