Leo277
Leo277

Reputation: 19

Writing an equals() method for Inheritance in Java

Im doing an inheritance question and I have everything completed except the Boolean part. My code will compile, but when comparing the name, birthday, and ssn, it will only come out as false.

For example, I inputted:

The output will be false.

public class Person
{
    private String name;
    private Date birthday;
    private int ssn;


    public Person(String name, Date birthday, int ssn)
    {
        this.name = name;
        this.birthday = birthday;
        this.ssn = ssn;
    }

    public String getName()
    {
        return name;
    }

    public Date getBirthday()
    {
        return birthday;
    }


    public int getSSN()
    {
        return ssn;
    }
     /* I already called a dates toString() method for birthday */
    public String toString() 
    {
        return name + " has birthday " +  birthday  + " and SSN " + ssn;
    }


    public boolean equals(Object otherObj)
    {
        if(otherObj == null)
            return false;
        else if(otherObj.getClass() != this.getClass())
            return false;
        else
        {
            Person otherP = (Person)otherObj;
            if(otherP.name.equals(this.name) &&
            otherP.birthday == this.birthday &&
            otherP.ssn == this.ssn)
                return true;
            else
                return false;
        }
    }
}

Upvotes: 1

Views: 944

Answers (2)

gparyani
gparyani

Reputation: 1988

I think the problem lies here:

if(otherP.name.equals(this.name) &&
        otherP.birthday == this.birthday &&
        otherP.ssn == this.ssn)

The code above is your main code for checking whether or not two Person objects are equal. You say your issue occurs when you supply two objects with the exact same birthdays, names, and SSNs. I'm not sure how your input code is creating the objects, but from what I can tell it's probably creating a new Date for every birthday.

There is an important difference between the == operator and the equals method. A variable of any type contains a value. For primitive data types (e.g. int), the value of the variable is the value it's actually storing. However, for an object variable (e.g. String, Date) the value is actually a reference to an object. If two object variables refer to the same object on the heap, they will have the same value; if they refer to different objects on the heap, they will have different values.

The == operator just checks the value of the variable, nothing else. When using it with primitives, it checks to see if the values stored in the variables are the same, which is proper because the values are stored directly. When using it with object variables, it checks to see if the two variables refer to the same object on the heap. Since the values of the variables are in fact references, this is the result when you check if the two values, or references, are the same.

The equals method, on the other hand, actually (is supposed to) go check further if two objects are considered equal, in that their states are the same. For example, it can check if two Date objects represent the same date. Java cannot magically tell if two different objects are equal - the class has to implement the equals method itself. (Otherwise, it resorts to a default implementation where it just checks the references.)

In your code sample above, you are using the == operator properly on the primitive ssn (since it is a primitive), and the equals method properly on the object name (since the references in each Person could be different, but represent the same string). However, you are using the == operator on the object variable birthday, which simply checks if the two Person objects being compared have the same Date object. Likely, since your calling code is just creating a new Date object on each input, the check fails because the two Person objects have two different objects for birthday. Just change that to an equals method call and your code should work.

Upvotes: 1

Dev Amitabh
Dev Amitabh

Reputation: 185

There is no inheritance here. In fact, writing equals method while inheriting from a superclass brings up a whole different discussion around class design (prefer composition over inheritance) .

In this case, I think you need to follow the rule of thumb, that is to always use equals method for logical comparison of all fields. == checks are for identity comparison and very seldom can work for logical equality (for example enums)

Another point to note is that your class will misbehave with datastructures that use hashing.

This topic is covered in good detail in chapter 3 of Effective Java by Joshua Bloch

Upvotes: 1

Related Questions