Notes - Imperative Programming TT23, Equality
Flashcards
class Rectangle(var width:Int, var height:Int) {
...
def ==(other: Rectangle): Boolean =
this.width == other.width && this.height == other.height
}
}
Can you give two reasons why implementing equality this way is wrong?
class Rectangle(var width:Int, var height:Int) {
...
def ==(other: Rectangle): Boolean =
this.width == other.width && this.height == other.height
}
}
- You’re only overloading the
==
method, rather than overriding theequals
method -
hashCode
remains unchanged, so collections will not work