ArrayLists and Iterators

25. Must Override the Right Method


Answer:

There is an error. getName() is not a method of Object.

Must Override the Right Method

You might wonder why the method is not written this way:

  public boolean equals( Entry other )
{
return name.equals( other.getName() );
}

This is a correct method, but it does not override the equals(Object) method. The types of the parameters do not match. To use indexOf(), you must override the equals(Object) method:


int indexOf(Object element)    //  Search for the first occurrence of 
// element, testing for equality
// using the equals(Object) method
// of element.

Question 25:

Will the following work for equals(Object) ?

    class Entry { . . . . . public boolean equals( Object other ) { return getName().equals( ((Entry)other).getName() ); } . . . . . }