lecture-09.Equality-Copying-Views - Lecture 9 Equality Copying and Views 9.1 The Object Contract Every class extends Object and therefore inherits all

lecture-09.Equality-Copying-Views - Lecture 9 Equality...

This preview shows page 1 - 3 out of 14 pages.

Lecture 9: Equality, Copying and Views 9.1 The Object Contract Every class extends Object , and therefore inherits all of its methods. Two of these are particularly important and consequential in all programs, the method for testing equality: public boolean equals (Object o) and the method for generating a hash code: public int hashCode () Like any other methods of a superclass, these methods can be overridden. We’ll see in a later lecture on subtyping that a subclass should be a subtype . This means that it should behave according to the specification of the superclass, so that an object of the subclass can be placed in a context in which a superclass object is expected, and still behave appropriately. The specification of the Object class is rather abstract and may seem abstruse. But fail- ing to obey it has dire consequences, and tends to result in horrible obscure bugs. Worse, if you don’t understand this specification and its ramifications, you are likely to introduce flaws in your code that have a pervasive effect and are hard to eliminate without major reworking. The specification of the Object class is so important that it is often referred to as ‘The Object Contract’. The contract can be found in the method specifications for equals and hashCode in the Java API documentation. It states that: · equals must define an equivalence relation – that is, a relation that is reflexive, sym- metric, and transitive; · equals must be consistent: repeated calls to the method must yield the same result unless the arguments are modified in between; · for a non-null reference x , x.equals (null) should return false; · hashCode must produce the same result for two objects that are deemed equal by the equals method; 61
Image of page 1
9.2 Equality Properties Let’s look first at the properties of the equals method. Reflexivity means that an object always equals itself; symmetry means that when a equals b , b equals -a ; transitivity means that when a equals b and b equals c , a also equals c . These may seems like obvious properties, and indeed they are. If they did not hold, it’s hard to imagine how the equals method would be used: you’d have to worry about whether to write a.equals(b) or b.equals(a) , for example, if it weren’t symmetric. What much less obvious, however, is how easy it is to break these properties inadver- tently. The following example (taken from Joshua Bloch’s excellent Effective Java: Programming Language Guide , one of the course recommended texts) shows how sym- metry and transitivity can be broken in the presence of inheritance.
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture