3
I have a parent class and in it I have a equals
and I have other daughter classes of this father class and in them I want to overwrite the equals
to compare particular attributes of these daughter classes but also want to make use of the equals
of my father to compare the attributes inherited by my daughters, how would I do that without having to rewrite everything?
public class Pai {
String nome;
@Override
public boolean equals(Object obj) {
Pai other = (Pai) obj;
return(this.nome.equals(other.nome);
}
}
public class Filha extends Pai {
int atributoEspecifico`;
@Override
public boolean equals(Object obj) {
// como comparo aquele atributo do meu pai também?
Filha other = (Filha) obj;
return this.atributoEspecifico == other.atributoEspecifico;
}
}
You can put a passage that exemplifies this?
– Maniero
sure, I’ll post.
– Leonardo Villela
puts a stretch, for better understanding
– user17270
You tried to compare the parent class attribute and it didn’t work? You tried
return this.atributoEspecifico == other.atributoEspecifico && this.nome.equals(other.nome)
? Thenome
is visible throughout the package, then it should be visible in the daughter unless the daughter is in another package. Tried to useproteceted
in thenome
? If you do this there if I’m not mistaken you’ll have to access withsuper
.– Maniero
@bigown I think the key point here is "without having to rewrite everything". Even if the
nome
is accessible in the subclass, it would not be a good idea to repeat the logic ofequals
if what the AP wants is to reuse it (in other cases it could be). P.S. I used the reference in your comment on the deleted reply in my reply.– mgibsonbr