Grade 10 Computer Science · Objects
The class is a mould, not the thing itself
The difference between the mould and what comes out of it.
A thousand students, and one fix for all
You fix a bug in the average, and at a stroke all thousand students get a correct average. But you add a grade for one student, and no other student is affected. How can the same program share something among all of them, and at the same time keep something separate for each?
This is the distinction that confuses most at first, and it is simple the moment it gets the right analogy. The class is the mould — it says which fields there will be and which operations are possible, and holds no one’s data. The object is what came out of the mould — it has values of its own, and they are its alone.
a = Student("Dana")
b = Student("Omar")
a.add(90)
a.add(80)
b.add(70)
print(a.average()) # 85.0
print(b.average()) # 70.0
print(a is b) # False — two separate objectsWhat the constructor does
The special operation that runs at the moment of creation is called a constructor. Its role is one: to bring the object into a valid initial state. This is an opportunity with no equal — it is the only moment at which no one has yet used the object. Any field you do not initialise here may turn up empty at a later stage, in a distant place, with no hint of where it came from.
| The class | The object |
|---|---|
| Written once | Created as many times as wanted |
| Holds no data | Holds its own data |
| Describes what is possible | Is an actual case |
Example 1 — a change on one
- A grade is added to Dana.
- Her grade list grows.
- Omar’s list was untouched.
- Because each object has a separate copy.
Example 2 — a fix in an operation
- A bug in the average is fixed.
- The code is written once in the class.
- So all the objects are fixed.