0

I intend to override the boolean equals(Object otherObject) method within my Pair class that utilizes a private inner class with private instance variables as follows:

class Pair
{
    class Node
    {
        private int x, y;
    }

    public boolean equasl(Object otherObject)
    {
        if(otherObject == null)
        {
            return false;
        }
        else if(getClass() != otherObject.getClass())
        {
            return false;
        }
        else
        {
            Pair other = (Pair)otherObject;

            return (x.equals(otherObject.x) && y.equals(otherObject.y));
        }
    }
}

It is not clear to me how I am comparing two Pair objects in which each object is comprised of a doubly-linked list (not shown for clarity). Am I comparing each object beginning with the head node and traversing the lists verifying that each node in the lists are equal?

Mushy
  • 2,535
  • 10
  • 33
  • 54

2 Answers2

1

The Pair class is comparing its value using equals with another pair, rather than doubly linked list. It takes the object which is a Pair object, then checks for null and classtype and finally compares x and y values of Node class inside another pair object.

hrv
  • 925
  • 6
  • 13
  • Your comment is right-on. I stumbled into the solution by adding an overridden equals method to the Node class that directly compares the x and y. The Overridden equals in Pair directly compares Pair objects and that results in a direct comparison of their x and y when the program is run. – Mushy Dec 07 '13 at 21:59
  • I had made a mistake, the x and y variables can be accessed anywhere inside the class directly, its only an error if its outside the class (doesn't matter what instance variable it is) – hrv Dec 07 '13 at 22:20
-1

Let's assume you have an instance variable Node n which is an instance of the inner class. I would recommend restructuring your equals method a little bit:

public boolean equals(Object other) {
 if (other == null)
  return false;
 if (! other instanceof Pair)
  return false;

 ...
}

Beyond that, you need to structurally compare the Node instances inside of each class. Since those are private variables on the Node, you won't be able to access them for other. It might be helpful to define get methods and then just compare the integers.

This is a good guide in general to overriding equals

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC0QFjAA&url=http%3A%2F%2Fwww.seas.upenn.edu%2F~cis120%2F13fa%2Flectures%2Flec32.pdf&ei=TI6jUvLTBeeysQSZxoGQCA&usg=AFQjCNFWIq0POyO5CGD7jCTbmKxlYyU5iQ&bvm=bv.57752919,d.cWc

  • Using `instanceof` in equals is bad practice. – Pavel Horal Dec 07 '13 at 21:16
  • I heard that instanceof in equals is bad before, but I've never find any good article about that. Can you give us a link? Thanks. – István Őri Dec 07 '13 at 22:02
  • 1
    It is simple... `equals` by definition should form *equality relation*. With `instanceof` you can not guarantee that it will be *symetrical*. I will try to search for some formal article about it. – Pavel Horal Dec 07 '13 at 22:10
  • 1
    It's been discussed: http://stackoverflow.com/questions/596462/any-reason-to-prefer-getclass-over-instanceof-when-generating-equals – Radiodef Dec 07 '13 at 22:12
  • 1
    Perhaps, it is, but in most cases it should be ok: http://www.artima.com/lejava/articles/equality.html – user3078468 Dec 07 '13 at 22:17