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?


  • You’re only overloading the == method, rather than overriding the equals method
  • hashCode remains unchanged, so collections will not work

Proofs




Related posts