2

Here is the main class.

public class Person {
private String name;

public Person() {
    name = "";
}

Its equal method is this

public boolean equals(Object otherObject) {
    boolean isEqual = false;
    if(otherObject != null && otherObject instanceof Person) {
        Person otherPerson = (Person) otherObject;
        if(this.name.equals(otherPerson.name))
            isEqual = true;
    }

    return isEqual;
}

I have a subclass that extends the Person class.

public class Student extends Person {
private int studentNumber;

public Student() {
    super();
    studentNumber = 0;
}

How would I go about writing its equal method that will compare two object of type Student and will compare both variables. Name and studentNumber. So far i have it similar to the Person class.

public boolean equals(Object otherObject) {
    boolean isEqual = false;
    if(otherObject != null && otherObject instanceof Student) {
        Student otherPerson = (Student) otherObject;
        if(this.studentnumber.equals(otherPerson.studentnumber))
            isEqual = true;
    }

    return isEqual;
}
  • 1
    You do `studentnumber.equals`, yet `studentnumber` is a primitive. I think you should read up on some Java tutorials online to get a better understanding of the laguage. You would use `==` to compare primitives. You also never compare the names, which would be done via `equals`. On top of all of this, you should read into [overriding the equals method](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) – Dioxin Nov 21 '15 at 16:57
  • A comment on implementing `equals`: if your class is not final (i.e. other classes may extend it), you should query the other object on class-equality: `this.getClass(otherObject.getClass())`. The keyword `instanceof` does cover inheritance and can therefore lead to asymmetric behaviour (`a.equals(b) == true`, but `b.equals(a) == false`). – Turing85 Nov 21 '15 at 16:57
  • @Turing85 that's the opposite; if a `Student` is also a `Person` then using `getClass()` will make any `Student` not equal to a `Person` if you rely on your superclass's `equals()`. `instanceof` is the thing to use here. – fge Nov 21 '15 at 17:01
  • @fge in my oppinion a `Student`-object should never be equal to a `Person`-object (they are not of same type and therefore not equal). Nevertheless, [you should always guarantee, that the relation is symmetric](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)), otherwise you break the contract of this method. – Turing85 Nov 21 '15 at 17:03
  • @Turing85 that's a given that .equals() is symmetric; and if you use .getClass() on Person then the relation is not symmetric. What is more, nothing guarantees that Student itself is not inherited. – fge Nov 21 '15 at 17:05

2 Answers2

1

First of all: you need not test for null if you test instanceof; null is not instanceof anything. So, equals() for method can be simplified to:

public boolean equals(final Object obj)
{
    if (!(o instanceOf Person))
        return false;
    final Person other = (Person) obj;
    return name.equals(other.name);
}

THis means that in your Student class you may write your equals() method like this:

public boolean equals(final Object obj)
{
    if (!super.equals(obj))
        return false;
    if (!(obj instanceof Student))
        return false;
    return studentnumber == other.studentnumber;
}

Note: do not forget hashCode().

fge
  • 110,072
  • 26
  • 223
  • 312
0

You can use super.equals(). I have corrected some errors also with the studentNumber comparision.

public boolean equals(Object otherObject) {
    boolean isEqual = false;
    if (otherObject != null && otherObject instanceof Student) {
        Student otherPerson = (Student) otherObject;
        if (super.equals(otherObject) && this.studentNumber == otherPerson.studentNumber) {
            isEqual = true;
        }
    }

    return isEqual;
}
11thdimension
  • 9,084
  • 1
  • 20
  • 60