Hi guys, my lecturer has mentioned overriding the equals method before. Am I correct in saying that you override the equals method so that instead of checking if two objects have the same reference it checks their values?

And therefore is it also right that there is only one code for overwriting it? if so could you guys demonstrate what that code is?

8 Years
Discussion Span
Last Post by ~s.o.s~

I'm not quite sure I understand your question exactly, but when you are overriding something there are infinite possibilities depending on what you are wanting to do.

For instance, I have an object and I want to change how toString() works instead of just giving me random information. Lets say I want it to actually print out the information in it with commas between the values. The code would look like so:

public String toString() {
        StringBuffer b = new StringBuffer();
        b.append(getStringA() + "," + getStringB() + "," + getStringC());
        return b.toString();

To my knowledge no, there isn't only one way to code an override. It really depends on what you want to do with it. You will however need the @Override though.

Now specifically for Overriding .equals() so that it compares objects or values of objects, most code will probably be similar.

(if my post is inaccurate or one feels the need to elaborate feel free to post so, constructive criticism is a great learning tool)

Edited by KirkPatrick: n/a


You will however need the @Override though.

You do not need the @Override. All you need to do to override the equals method is provide this method header (and an implementation) in your class:

public boolean equals(Object obj){
//some implementation goes here

Using the @Override tag is helpful, though, because by putting the @Override tag above the method header, the compiler will tell you if you have not properly overridden the method (if you didn't override anything, it gives an error I think).


The way you implement the equals method depends on what the properties of your class are. For example, if you had a Square class, you know that any two squares with a side of the same length are the exact same square. So your equals method would simply check to see if the calling square's side is the same length as the square passed in. If they are, then the equals method would return true. Otherwise, it'd return false. For example, here is an implementation of a sample class called Square (that I'm making up right now). Square overrides the equals() method, enabling you to compare two Squares to see if they are equal.

public class Square{
private int sideLength;
//I'm too lazy to show the constructor, pretend a working one is here.

public static void main(String[] args){
Square whatever = new Square(5);
Square another = new Square(5);
boolean equalSquares = whatever.equals(another);

public boolean equals(Object obj){
if (this == obj) return true; //They have the same reference
if (!(obj instanceof Square)) return false; //obj is some other object, not a Square

Square sq = (Square)obj;
if (sq.sideLength == this.sideLength) return true;
else return false;


Hope that helps.


Also make sure that you override the hashCode method for your custom class to satisfy the hash code contract.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.