Part 6 of 7 · 1 min read
What to model
A class needs a job.
Choosing what to model as a class
A class should have a reason. GoF describes the design task as finding the right objects: deciding what should be an object is hard, because many factors apply, such as encapsulation, granularity, dependency and flexibility. A practical test from Level 0 remains: each class needs a requirement behind it.
Interview angle
Answer in your head first, then open it.
What is the difference between a class and an object?
A class is the definition. An object is one instance with its own data.
What is the difference between == and equals?
== compares identity, equals compares logical value.
Why override hashCode with equals?
Hash collections use the hash first, so equal objects must have equal hash codes.
When should a class not override equals?
When each instance is unique, or nobody needs logical equality.
Check yourself:
1. What decides whether something deserves a class?
2. What does the equals contract prevent?
3. When can the default equals be kept?
Words you will meet
| Word | Meaning |
|---|---|
| Class | The definition of a kind of object |
| Object | One instance of a class |
| State | The data an object holds now |
| Behaviour | The operations an object can perform |
| Identity | What makes an object this one and not another |
| Constructor | The method that builds an object |
| Value class | A class that stands for a value |
| Hash code | A number used to find an object in a hash collection |