Esempio

public InstanceofEquals_Example( String str ) {
super();
this .str = str;
}

public String getString() {
return str;
}

public int hashCode() {
return str.hashCode();
}

public boolean equals(Object other){
return str.equals( ( (InstanceofEquals_Example)other ).getString() );
}

private String str;

Soluzione
Utilizzare instanceof in equals() prima del casting.

public InstanceofEquals_Solution( String str ) {
super();
this .str = str;
}

public String getString() {
return str;
}

public int hashCode() {
return str.hashCode();
}

public boolean equals(Object other) {
return ( other instanceof InstanceofEquals_Solution) &&
str.equals( ( (InstanceofEquals_Solution)other ).getString() );
}

private String str;