0

If two employee objects are the same, then I want to check if the same number is returned by the "getID()" method. I think I'm using the equals method wrong. I think something is wrong with the last line of code:

I have to keep this code:

public boolean equals( Object b )
{
  if ( ! (b instanceof Employee) )
    return false;

this is what I can edit:

(super.equals().getID()).equals(b.getID());

I get this error:

Compile Result: Error: Employee.java:25: error: cannot find symbol

Thanks for the help. I appreciate it.

user2932
  • 137
  • 3

1 Answers1

0

If "number" returned by getID() is a primitive

public boolean equals( Object b ){
   if (this == b) return true; 
   if ( ! (b instanceof Employee) )
      return false;
   final other=(Employee)b;
   return getID()==other.getID();
}

if number is Object

public boolean equals( Object b ){
   if (this == b) return true; 
   if ( ! (b instanceof Employee) )
      return false;
  final other=(Employee)b;
  return getID().equals(other.getID());
}

If you override equals make sure you override hashCode() as well.

sorin
  • 1
  • 1