I have a superclass with 2 variables (int a
) (int b
) and have a subclass, which extends the functionality of the superclass by overriding 2 superclass methods with improved methods now in the subclass. As part of my subclass I now have a new int
(int c
) which is a unique id (UUID).
I understand equality is difficult to maintain with the equals
method
Can I override the Superclass equals
and hashCode
methods to show equality AND also do the same for the subclass equals
and hashCode
method based on my situation described above?
I had initially overriden the equals
and hashCode
methods in the superclass. If it supposed to be done for the subclass also because of the extra Instance Variable (int c
) in the subclass. I understand that there should be a hashCode
method to show this, and I read if the haschCode
is changed then the equals
method of the subclass has to change?
I am really getting confused as to what best to do.
I was wondering if my superclass equals
and hashCode
methods can show the equality relationships? Is it allowed in conjunction with my superclass equals
and hashCode
overriden methods that the subclass can show equals comparison for int a
, int b
and int c
and the hashCode
methods of the subclass updated to show a unique hashcode for int
s a
, b
and c
?
I was thinking of comparing my int
s a
and b
in the superclass equals
method and update hashcode for these 2 variables and int
s a
, b
and c
in the equals
method, updating hashcode for these 3 variable. It's just that int c
in the superclass is unique?
Very grateful for any advice here as I believe ignoring and not dealing with the int c
in the subclass equals
and hashCode
methods may be a no no.
Thanks in advance