1 of 17

CSE 331�Software Design & Implementation

​

Autumn 2026

Section 6 - Equals

2 of 17

Administrivia

Midterm this Friday 11/13 during lecture !!!!

- Mock exam (today): Tuesday 11/10, 4:30 - 6:20 PM

- Midterm review session: Thursday 11/12, 4:30 - 6:20 PM

- Both in CSE2 G01

​

3 of 17

Equals and hashCode

4 of 17

The equals method (review)

The contract that every override of Object.equals must satisfy:

​

Reflexive:

an object equals itself.

x.equals(x) == true

​

Symmetric:

reversing the operands preserves the result.

x.equals(y) == y.equals(x)

​

Transitive:

equality carries through a chain.

x.equals(y) && y.equals(z) implies x.equals(z)

​

Consistent:

repeated calls return same result, unless equality-relevant state changes

x.equals(y) // same result until mutation

​

Null uniqueness:

non-null objects must not be equal to null

x.equals(null) == false

5 of 17

Additional notions of equality

Reference equality:

references point to same object in memory

String a = new String(“hi”);

String b = a;

a == b; // true

​

Behavioral equivalence:

no sequence of ordinary operations can distinguish the objects

String a = new String(“hi”);

String b = new String(“hi”);

a.equals(b); // true

​

Observational equivalence:

cannot distinguish the objects without mutating them or testing identity

Date a = new Date(0);

Date b = new Date(0);

a.getTime() == b.getTime(); // true

6 of 17

The hashCode method (review)

The contract that every override of Object.hashCode must satisfy:

​

Self-consistent:

repeated calls return the same result unless equality-relevant state changes

x.hashCode() // same result until mutation

​

Equality-consistent:

equal objects must have equal hash codes

x.equals(y) implies x.hashCode() == y.hashCode()

​

​

Important:

  • must override hashCode whenever you override equals
  • unequal objects may still share a hash code

​

JavaDoc documentation for Object.hashCode

7 of 17

Method Overriding

  • A subclass method overrides a superclass method when:
    • The method names are identical
    • The parameter types are identical
      • Parameter names do not matter

​

  • An overriding method must satisfy the overridden method’s spec.

​

  • Use @Override tag to have compiler check that an override exists.

​

​

  • Overloading:
    • same method name with different parameter types
    • different from overriding

8 of 17

Overriding equals

  • Overriding requires the same method name and parameter types.

​

  • To override equals, the parameter must have type Object.

​

​

Overriding:

public bool equals(Object o) { ... }

​

public bool equals(Object d) { ... }

​

​

Overloading (not an override):

public bool equals(Whale o) { ... }

9 of 17

Task 1: Thing 1 and Thing 2

Consider the following class Thing, which, unfortunately, doesn't properly override equals, but overloads it instead.

class Thing {

private int contents;

public Thing(int value) {

this.contents = value;

}

public boolean equals(Thing other) {

return this.contents == other.contents;

}

}

10 of 17

Task 1: Thing 1 and Thing 2

For each method call, indicate which equals method runs, and whether it prints true or false.

public static void main(String[] args) {

Thing t = new Thing(17);

Object o = t;

Thing u = new Thing(17);

// Method called Output

System.out.println(t.equals(o));

System.out.println(t.equals(u));

System.out.println(o.equals(u));

System.out.println(u.equals(o));

System.out.println(u.equals((Thing) o));

}

​

11 of 17

Task 2: Wrecked Angle

Here is a small class with a correctly overridden equals method.

public class ColoredRectangle {

private int width;

private int height;

private String color;

/** Equality for ColoredRectangles. Two ColoredRectangles

* are the same if they have the same width and height.

*/

@Override

public boolean equals(Object o) {

if (! (o instanceof ColoredRectangle)) {

return false;

}

ColoredRectangle cr = (ColoredRectangle) o;

return this.width == cr.width && this.height == cr.height;

}

}

12 of 17

Task 2: Wrecked Angle

For each implementation: Does it satisfy the hashCode contract? If yes, identify one type of collision between unequal rectangles. If no, explain why.

(Recall: ^ is bitwise XOR.)

return width;

​

​

return color.hashCode();

​

​

return 42;

​

​

return width ^ height ^ color.hashCode();

​

​

return width ^ height;

13 of 17

Typical hashCode implementations

@Override�public int hashCode() {� int result = Integer.hashCode(size);� result = 31 * result + Integer.hashCode(weight);� return result;�}

@Override�public int hashCode() {� return Objects.hash(size, weight);�}

Two example implementations:

Correct and effective hash functions minimize collisions for expected inputs.

14 of 17

Exam Practice

15 of 17

Exam-taking strategies

  • Scan the entire exam first.
    • Identify easy, medium, and difficult problems.
    • Note point values and allocate your time accordingly.
  • Prioritize points, not problem order.
    • Prioritize completing questions you can answer confidently.
    • Don't waste time on a difficult problem when easier questions remain.
  • Set a time limit for each problem.
    • If you're stuck after a few minutes, mark the problem and move on.
    • Return if time permits.

16 of 17

Time to vote

  • Go here and vote on your top two question types.
    • Located at the bottom of the 331 Topics page

​

  • We highly recommend one equality question to reinforce what you just learned.

  • Use the blank pages at the back of your worksheet.

17 of 17

End

Please turn in your

half-sheets!