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?

Recommended Answers

All 3 Replies

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:

@Override
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)

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).

@OP:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.