Skip to content
Ameen Abozaid
BEG-026

Java object model

  • 6 Lessons
  • 60 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Object
  • equals()
  • hashCode()
  • toString()
  • identity vs equality

Learn first

60 min of reading · 3 h of practice (estimate)

Learning outcomes

  1. Explain that every class extends Object and name its key methods.
  2. Distinguish identity (==) from equality (equals) and fix string comparison bugs.
  3. Implement equals and hashCode consistently and explain the contract.
  4. Override toString for useful logging and debugging.

Lessons

  1. 01The Object class10 min read
  2. 02Identity vs equality10 min read
  3. 03The equals/hashCode contract10 min read
  4. 04toString10 min read
  5. 05Records as a preview: equality for free10 min read
  6. 06Demo: the broken HashSet and the equals/hashCode fix10 min read

Lab and exercises

Lab

BEG-026-LAB1 · Broken set

Outcomes: 2, 3, 4 · Time: ~1.5 h Put Money objects into a HashSet (treat it as a black box) with no overrides, with only equals, and with both equals and hashCode. Record the set size each time and explain the duplicates. Acceptance: correct implementation and explanation.

Exercises (assessment: contract quiz plus implementation exercise)

IDOutcomeTask
BEG-026-EX011List five methods every object inherits from Object, and say which three are most often overridden.
BEG-026-EX022Predict == and equals for: two literals "a", new String("a") vs a literal, two new Account(...) with the same data and no overrides.
BEG-026-EX032Fix: if (command == "quit").
BEG-026-EX043Implement equals and hashCode for Customer(id, name) where id identifies the customer.
BEG-026-EX053Explain why a mutable field in hashCode can make an object "disappear" from a HashSet.
BEG-026-EX064Write a toString for Account that shows number and balance but not secrets.
Show solutions
  • EX01 toString, equals, hashCode, getClass, wait/notify (or clone). Most overridden: toString, equals, hashCode.
  • EX02 "a" == "a" true (shared literal), equals true; new String("a") == "a" false, equals true; two accounts: == false, equals false.
  • EX03 "quit".equals(command) (also safe against null).
  • EX04 Compare id only: equals checks o instanceof Customer c && id == c.id; hashCode returns Long.hashCode(id) or Objects.hash(id).
  • EX05 The object was stored under a bucket from its old hash; after the field changes, lookups use the new hash and look in another bucket.
  • EX06 return "Account[" + number + ", balance=" + balance + "]";
  • Outcome 3 is also covered by the lab and the quiz.

Quiz

Outcomes: O1 Object · O2 identity vs equality · O3 equals/hashCode · O4 toString.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Every Java class extends, directly or indirectly:O1
  2. 2Which method does Object NOT have?O1
  3. 3== on two object references checks:O2
  4. 4The correct way to compare two strings' text:O2
  5. 5The default equals from Object behaves like:O2
  6. 6If a.equals(b) is true, then:O3
  7. 7Overriding equals but not hashCode can cause:O3
  8. 8Which field is risky to use in hashCode?O3
  9. 9toString output should NOT contain:O4
  10. 10A record automatically gets:O3

Practical part

Contract quiz above plus implementation exercise BEG-026-EX04 and the broken-set lab. Pass: quiz 8/10 and a correct equals/hashCode pair with the duplicate behavior explained.

Common mistakes

Overriding equals without hashCode; using mutable fields in hashCode.

Assessment

Contract quiz plus implementation exercise.

Career relevance

Essential for collections (BEG-030), hashing (BEG-041), and JPA entities (JNR-028).

Type to search levels, phases, modules and lessons.