casting - I want to know the meaning of a line in java -
what meaing of below line of java code
class dog { int size; dog(int size) { this.size = size; } public boolean equals(object o) { return this.size == ((dog) o).size; // im not getting whats meaning of line } } i want know meaning of following line:
return this.size== ((dog)o).size;
this meaning: current object's size variable compared equality other object's size variable. result, boolean value, returned result of equals method.
an implicit assertion made other object of same type. proper implementation of equals must not throw classcastexception in case, rather return false. therefore implementation not comply contract of object#equals method.
to me looks developer feeling smart , "found out" way concisely implement equals. correct, still quite concise, implementation go this:
return o instanceof dog && ((dog)o).size == this.size;
Comments
Post a Comment