0

I am trying to override the toEquals method for a pojo. However I am not sure how I am able to compare the parent class attributes since super() is throwing me an error.

I am trying something like this:

@Override 
public boolean equals(Object o) {
  return super.equals(o.super()); // it mentions that the abstract parent class is not an inner class
}

how do I compare the parent abstract class fields for equality as well? There is a chance that the child attributes are similar, but the parent attributes will always be different.

AKJ
  • 749
  • 6
  • 29

1 Answers1

0
@Override 
public boolean equals(Object o) {
  // compare local fields and return false if some of them are not equal
  
  // if all fields are equal in the current class, then delegate work to the parent class.
  // You should not care what it does, just call it and retrieve the result
  return super.equals(o);
}

P.S. If you use an IDE like IDEA, you can generate equals() and hashCode() automatically. As another alternative, you can use Project Lombok.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Wouldn't this compare the parent class of the "this" object to the child class of the "that" Object o? – AKJ Apr 05 '21 at 06:20
  • I used the IDE's generate function for the equals and the hashCode methods. But the equals only compares the attributes belonging to the child class and not to its abstract parent class – AKJ Apr 05 '21 at 06:22