I have a abstract entity class that 3 slightly different entities implements. In my 3 sub classes I have overriden the equals and has methods but the question is, should I also do this in the abstract entity? If I dont I will not be able to compare entities that are only defined by abstract entity unless i cast them. If i do a equals will I risk to compare to different sub entities and get that they are alike?
Example:
abstract class Log{}
SystemLog extends Log{}
UserLog extends Log{}
public void test(Log log){
Log myInner = new SystemLog();
if(log.equals(myInner)){
//do random stuff
}
}
I cannot see problem with casting. Type of argument to equals is Object, so you have to cast anyway to have access to attributes.
If you define equals method in each subclasses, when comes the situation where equals in abstract superclass is called?
You are in the risk of comparing different subentities to each others anyway. Just imagine Set with superclass as type populated with objects that are two instances of two different subclasses. It has not too much to do with do you override equals in superclass or not.
In your example equals method possibly implemented in abstract class Log will not be called, if we have implementation already in actual subclass:
Assuming: