Community resourceWorksheet

OCR H446 2.2.1 Encapsulation and getters/setters

Part 10 of 14 · H446 2.2.1 · Programming techniques

Encapsulation is easiest to explain through a rule that has to hold, so this H446 2.2.1 worksheet fixes a check-in score to the range 1 to 5 and asks what keeps it there. Students correct the getter and setter faults that recur in exam answers, then implement validated access that refuses an out-of-range update.

Students will:

  • explain encapsulation in terms of a rule the object must keep true
  • recognise why a getter that does not return a value fails as a function
  • rewrite a getter and setter in OCR exam reference language with a proper parameter and return
  • implement a setter that validates its input and leaves state unchanged when it refuses
  • explain how controlled access reduces coupling to a class's internal representation

Inside: 7 explanation cells, 1 multiple-choice question, 1 fill-in-the-blanks cell, 2 written answers and 1 Python task. 19 marks, about 25 to 35 minutes.

Series: H446 2.2.1 · Programming techniques, part 10 of 14.

Shared by Coding PathwayVerified teacher

  • 12 cells
  • About 30 minutes
  • CC BY-SA 4.0
  • Shared 31 Aug 2026
  • Updated 3 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Encapsulation and getters/setters

A check-in object must keep its score within 1 to 5. Controlled methods protect that rule and hide representation decisions.

By the end, you will be able to

  • explain encapsulation through a maintained invariant
  • write a returning getter function
  • write a parameterised, validating setter
  • explain reduced coupling with a concrete consequence

Reactivate: classes, constructors, attributes and methods.

Access methods

Encapsulation groups state and behaviour while limiting direct access. A getter is a function that returns an attribute. A setter takes a new value as a parameter and assigns it, often after validation. Private notation communicates a design contract; enforcement varies by language.

Worked access model

Direct write: object.score = 9 bypasses the 1–5 rule.

Controlled call: object.set_score(9) checks the range, refuses the update and leaves the old state intact.

A correct OCR getter has three visible parts: function declaration → access attribute → return value. A setter has parameter → validation/assignment → stated outcome. This checklist catches the missing return and missing parameter faults highlighted in OCR evidence.

Multiple choice1 mark

Which OCR-style description defines a correct getter for score?

  • AA procedure with no return
  • BA function that returns score
  • CA constructor that resets score
  • DA public variable outside the class

Correct recurring faults

OCR ERL listing:

procedure getScore()
    score
endprocedure

procedure setScore()
    score = newScore
endprocedure

The getter is not a returning function. The setter lacks a parameter, so newScore has no valid source.

Written answer4 marks

Rewrite getScore and setScore in OCR ERL so the getter returns score and the setter receives newScore then assigns it.

Use a function for the getter and a parameter for the setter.

Students type their answer here.

Implement controlled access

Complete CheckIn. set_score updates only for 1 to 5 inclusive and returns True; otherwise it leaves state unchanged and returns False.

Coding task6 marks
class CheckIn:
    def __init__(self, display_name):
        self._display_name = display_name
        self._score = 3

    def get_score(self):
        pass

    def set_score(self, new_score):
        pass
Written answer4 marks

Explain how using set_score rather than direct writes helps maintain the class invariant and reduces coupling.

Connect controlled access to the 1–5 rule and future internal changes.

Students type their answer here.

Checkpoint

Complete the controlled-access explanation from memory. There is no answer bank, and correctness is withheld until teacher review.

Fill in the blanks4 marks
Encapsulation limits direct access to checkpoint gap 1 state. A checkpoint gap 2 is a function that returns an attribute. A checkpoint gap 3 receives a new value and may validate it. The rule kept true by that control is an checkpoint gap 4.

Review your understanding

Before submitting, check that you can explain the main distinction in your own words, apply it in an unfamiliar context and justify the resulting behaviour or consequence.