1

I am writing an equals method in which two names are considered equal if they have the same first, middle, and last names. However, I keep getting the error

"This class defines a covariant version of the equals() method, but inherits the normal equals(Object) method defined in the base java.lang.Object class. The class should probably define a boolean equals(Object) method."

and when I change it to Object other as the parameter, I get a "no such method" error.

public boolean equals(Name other) {

    boolean sameFirstName = firstName.equals(other.firstName);
    boolean sameMiddleName = middleName.equals(other.middleName);
    boolean sameLastName = lastName.equals(other.lastName);
    if (sameFirstName && sameMiddleName && sameLastName) {
        return true;
    }
    return false;
}
DagdA
  • 484
  • 1
  • 7
  • 25
neoiajr12
  • 13
  • 2

2 Answers2

1

You have to use Object type for parameter which called 'other'. Then do instance of checking and casting. Please refer to this answer

If you use Java 7 or higher, you can use this code:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Person person = (Person) o;
    return Objects.equals(firstName, person.firstName) &&
           Objects.equals(middleName, person.middleName) &&
           Objects.equals(lastName, person.lastName);
}

And there is an automatically generated equals in IDE:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Person person = (Person) o;

    return !(firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) 
                && !(middleName != null ? !middleName.equals(person.middleName) : person.middleName != null) 
                && !(lastName != null ? !lastName.equals(person.lastName) : person.lastName != null);

}

Please notice that there should be NPE check as well.

Community
  • 1
  • 1
ayurchuk
  • 1,879
  • 1
  • 17
  • 13
0

You are overloading the method equals by having a different parameter to type Object. The following should work for you.

@Override
public boolean equals(Object object) {
    if(this == object) return true;
    if(!(object instanceof Name)) return false;

    Name other = (Name) object;
    boolean sameFirstName = firstName.equals(other.firstName);
    boolean sameMiddleName = middleName.equals(other.middleName);
    boolean sameLastName = lastName.equals(other.lastName);
    if (sameFirstName && sameMiddleName && sameLastName) {
        return true;
    }
    return false;
}  
DagdA
  • 484
  • 1
  • 7
  • 25
  • the project description says to name it equals – neoiajr12 Nov 26 '15 at 23:30
  • @neoiajr12 No problem. Glad I helped. :) If this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – DagdA Nov 29 '15 at 20:54