This is possibly a code smell but I was wondering if this might be possible in Java. Given that I have my interface:
public interface State {
String getStateName();
}
and I have one implementation like this:
public class DefaultState implements State {
final String stateName;
public DefaultState(String stateName) {
this.stateName = stateName;
}
@Override
public String getStateName() {return stateName; }
@Override
public boolean equals(Object other) {
return ((State)other).getStateName().equals(this.stateName);
}
}
and another like this:
public enum EnumState implements State {
STATE_1("STATE1"),STATE_2("STATE_2");
final String stateName;
EnumState (String stateName) {
this.stateName = stateName;
}
@Override
public String getStateName() {return stateName; }
}
When I do the following it fails because I cant override how equals
is implemented in the enumeration:
assertTrue(Arrays.asList(new DefaultState("STATE1")).contains(EnumState.STATE_1)); // fails
Is there a way of making this work or is the ultimate answer you shouldn't be mixing implementations like that?