I have an issue where I am trying to use test the equals
method of a superclass using objects of a subclass:
My superclass is:
public class Excluded_DateTime implements Serializable {
private Date fromDate;
private Time fromTime;
private Date toDate;
private Time toTime;
private Valid active;
And the subclass differs by having an identifier as a key:
public class Classifier_Excluded_DateTime implements Serializable {
private Integer classifierExcludedDateTimeNo;
private Excluded_DateTime classifierExcludedDateTime;
So I want to test the equality of Classifier_Excluded_DateTime
objects without using the field classifierExcludedDateTimeNo
.
But what I am finding is that the equals
method of the superclass is never called.
The NetBeans generated equals
and hashCode
methods of the superclass are as follows:
@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (this.fromDate != null ? this.fromDate.hashCode() : 0);
hash = 23 * hash + (this.fromTime != null ? this.fromTime.hashCode() : 0);
hash = 23 * hash + (this.toDate != null ? this.toDate.hashCode() : 0);
hash = 23 * hash + (this.toTime != null ? this.toTime.hashCode() : 0);
hash = 23 * hash + (this.active != null ? this.active.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Excluded_DateTime other = (Excluded_DateTime) obj;
if (this.fromDate != other.fromDate && (this.fromDate == null || !this.fromDate.equals(other.fromDate))) {
return false;
}
if (this.fromTime != other.fromTime && (this.fromTime == null || !this.fromTime.equals(other.fromTime))) {
return false;
}
if (this.toDate != other.toDate && (this.toDate == null || !this.toDate.equals(other.toDate))) {
return false;
}
if (this.toTime != other.toTime && (this.toTime == null || !this.toTime.equals(other.toTime))) {
return false;
}
if (this.active != other.active) {
return false;
}
return true;
}
And those of the subclass as follows:
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (this.getClassifierExcludedDateTimeNo() != null ? this.getClassifierExcludedDateTimeNo().hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
if (! super.equals(obj)) {
return false;
}
final Classifier_Excluded_DateTime other = (Classifier_Excluded_DateTime) obj;
if (this.getClassifierExcludedDateTimeNo() != other.getClassifierExcludedDateTimeNo() && (this.getClassifierExcludedDateTimeNo() == null || !this.classifierExcludedDateTimeNo.equals(other.ClassifierExcludedDateTimeNo))) {
return false;
}
return true;
}
Can I do what I'm trying to do by amending these methods of either class?
I am trying to test whether two instance of Classifier_Excluded_DateTime
are the same without including the field classifierExcludedDateTimeNo
in the comparison.
You are not declaring Classifier_Excluded_DateTime
as a subclass of Excluded_DateTime
. To do that, you would need to say:
public class Classifier_Excluded_DateTime extends Excluded_DateTime {
Right now, it seems more like you are treating Excluded_DateTime
as a kind of delegate of Classifier_Excluded_DateTime
, as you have a member instance variable inside of Classifier_Excluded_DateTime
that is of type Excluded_DateTime
(the place where you say private Excluded_DateTime classifierExcludedDateTime;
). I don't think that's your intent, since you are talking about subclasses and superclasses in your post.
(Update: removed error where I thought there was not an explicit super.equals()
call).