How to compare objects

  • To test if two objects point to the same space in memory, you can use the equals mthod of the Object class.
  • To test if two objects store the same data, you can override the equals method in the subclass.

Override the equals method of the Object class.

public boolean equals(Object object)
{
if (object instance of Product)
{
Product product = (Product) object;
if (code.equals(product2.getCode())
return true;
}
return false;

}