081121slides_handouts

081121slides_handouts - What is Equality? Equality Simple...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 UVa CS205 Engineering Software 081121 Equality UVa CS205 Engineering Software 081121 What is Equality? ± Simple question, complex answer ± Temporality: ² If two objects are equal, does that mean they’ll always be equal? ± Equality and inheritance: ² Can objects be equal at one level and not equal at another? ± Containment: ² Is equality of collections different from equality of elements? ± Flexibility: ² How can a simple concept (equality) be maintained in an evolving system? Time Æ Subclass Æ UVa CS205 Engineering Software 081121 Varieties of Equality (review) ± Three types of equality ² reference equality (more equal) ± two names pointing to same box ² behavioral equality (less equal) ± two boxes with same picture (final) ² observational equality (least equal) ± two boxes with same picture ± Two ways of expressing equality ² == ² equals() Some objects are more equal than others (apologies to George Orwell) UVa CS205 Engineering Software 081121 Equality semantics ± == ² Generally means reference equality ² Can be observational (as applied to primitive types) ± equals() is less clear ² Reference equality as defined in object ± inherited by every class that doesn’t override it ² Liskov believes it should be behavioral equality ² In practice, often observational equality.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 UVa CS205 Engineering Software 081121 Inheriting equals() ± Should a new class inherit equals() from object? ² Mutable objects can: ± Results will correspond with behavioral equality semantics ² Two mutable objects are equals() only if they are the same object. ± Liskov proposes similar() if user means to test for content equality: ² Immutable objects need to override equals() because two immutable object instances should be equals() if their contents are equal. ± Equals() and similar() should produce same results for immutable objects. Intset s = new Intset(); Intset t = new Intset(); if s.equals(t) // always fails Intset s = new Intset(); Intset t = new Intset(); if s.similar(t) // succeeds UVa CS205 Engineering Software 081121 A Can of Worms? ± Buying into behavioral equality for mutable objects can have repercussions. ² e.g. set equality is often argued by showing that two sets are subsets of each other: ² If subset test should fail then we’d need to define a method “similarToSubset”? ± Or make our Intsets immutable
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/05/2010 for the course CS 150 taught by Professor Humphreys,g during the Spring '08 term at UVA.

Page1 / 6

081121slides_handouts - What is Equality? Equality Simple...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online