3
In that code:
public boolean equals(Object o) {
Aluno outro = (Aluno)o;
return this.nome.equals(outro);
}
What’s he good for?
3
In that code:
public boolean equals(Object o) {
Aluno outro = (Aluno)o;
return this.nome.equals(outro);
}
What’s he good for?
3
equals(Object o)
is, like the bfavaretto I have already said, part of the signature of the method - here has more details about method signature. This defines the method name (equals
) and its parameters (a Object
called o
).
The method equals
is standard of the class Object
(all classes in Java inherit from Object
) which usually has the function of defining whether an object is equal to another, so it asks for an object as a parameter.
I couldn’t help but notice that the method code is wrong, it will always return false
, because it is comparing an attribute of the current object with the object that was passed by parameter, see:
return this.nome.equals(outro);
//this.nome => objeto atual
//outro => objeto passado por parâmetro
If the intention is to compare attributes nome
, the code should be
return this.nome.equals(outro.nome);
Browser other questions tagged java oop inheritance
You are not signed in. Login or sign up in order to post.
equals(Object o)
is the signature of the method.equals
is a public method that takes an object and returns a boolean value. That’s what you want to know?– bfavaretto
I don’t know much
java
,equals
wouldn’t that be a private word? @bfavaretto– rubStackOverflow
equals
is a class methodObject
, every java class inherits fromObject
implicitly.– Renan Gomes
Nope,
equals
is a class methodObject
@rubStackOverflow– Jéf Bueno
I don’t get it, I’m reading a booklet from Home of algorithms and data structure.
– user41087
Is this code right? It seems that you are comparing an object attribute with the other whole object. It wouldn’t be
this.nome.equals(outro.nome)
?– Renan Gomes
That, I forgot to put the . name on this.nome.equals(other.name)
– user41087