The big idea: Defining a class writes the template. Instantiating creates an actual object from it.
The class is written once; objects are created as often as you need them.
The big idea: Defining a class writes the template. Instantiating creates an actual object from it.
The class is written once; objects are created as often as you need them.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
It runs automatically
- Called when the object is created, never by you directly
- Python names it __init__; Java names it after the class
- Whatever you pass at creation arrives as its parameters
It sets the starting state
- Gives every attribute a value before anything can use it
- So no object ever exists in an unfinished state
- onLoan starts False — a new book is not on loan
It can refuse
- Checks can be made before the object exists
- An empty title can be rejected at creation
- Far better than allowing a broken object and hoping
What self means: self is the object the method was called on.
self.title is this book's title; another object's method call has a different self. It is what lets one method definition serve every object.
What self means: this is the object the method was called on.
this.title is this book's title; another object's method call has a different this. It is what lets one method definition serve every object.
Not every attribute is a parameter: title and author come from outside, because they differ per book.
onLoan is set inside to a sensible default — a new book is never on loan, so asking the caller would be pointless and error-prone.
See how examiners mark answers
Access past paper questions with model answers. Learn exactly what earns marks and what doesn't.
Every method takes self
Methods return, rather than print
Objects behave like any value
Every method has a this
Methods return, rather than print
Objects behave like any value
Java, briefly: Java names the constructor after the class — public Book(String title) — and writes this.title where Python writes self.title.
The structure and the reasoning are the same; only the spelling differs.
How this is tested — you must write a constructor that sets every attribute, and methods that return. It comes up two ways:
Paper 2 — working with code
- Construct a class with a constructor and methods, 4-6 marks
- Create objects and call their methods
- Explain what the constructor does
Paper 2 — the algorithmic-thinking question
- Explain the role of a constructor with no code shown
- Say why an attribute has a default rather than a parameter
The classic trap: Forgetting to set an attribute in the constructor. An object then exists with a missing value, and the failure appears much later — somewhere that has nothing to do with the cause.
Write a Student class holding a name and a list of marks, with a method to add a mark and one to return the average. Create two students and use both methods.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
Write a Student class holding a name and a list of marks, with a method to add a mark and one to return the average. Create two students and use both methods.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.