I have a superclass with 2 instance variables (say int a
, and int b
) and
have now decided I need to have one instance variable in my subclass int c
.
I had overriden the superclass equals and hashCode method to take into account equality of objects/values. I have also overriden the superclass toString to give a string representing the state of the objects (variable values).
Based on the above and the fact I now have a single instance variable int c in my subclass I was wondering if I
- need to override the toString method in the subclass or is this a no no as I have a valid inheritable toString method (already overriden) in the superclass that can be used for the subclass string representation?
- need to override the equals method in the subclass. Can this be done by calling the superclass equals method and adding the comparison code for my subclass instance variable after the call, or does it require a new implementation? I figure the latter?
- I was going to override the hashCode method in the subclass. Again is it best to have a new implementation than call the superclass hashCode method? I was going to compare a, b and c in the subclass equals method with another object and also create hash codes for a b and c in the subclass hashCode method? Is this the way to go or does it require me to only consider variable c in the subclass equals and hashcode methods?
I have had a look at various resources including Effective Java by bloch (chapter 3) but could not find the answer to the above questions that I have