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 35 to 50 minutes.

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

Shared by Coding PathwayVerified teacher

  • 12 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 31 Aug 2026
  • Updated 1 Oct 2026

Preview

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

Encapsulation and getters/setters

Encapsulation groups an object's data with the methods that work on it and controls access from other code. A check-in object uses methods to keep its score between 1 and 5. This required rule is called an invariant.

By the end, you will be able to

  • explain how encapsulation helps keep object state valid
  • write a getter function that returns an attribute
  • write a setter that receives and validates a new value
  • explain one practical benefit of controlled access

Access is about program code

Encapsulation groups data and the methods that work on it, while controlling how other code can access that state. Public and private describe access from program code; public does not mean visible to every end user.

A getter is a function that returns an attribute. It normally needs no parameter because it reads state already held by the object. It should not ask for input or overwrite the attribute. A setter receives a new value as a parameter and may validate it before updating the attribute. Private notation communicates a design contract; the exact enforcement depends on the 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 → read the attribute → return its value. It does not need a newScore parameter. A setter has parameter → validation → assignment only when valid. This checklist catches missing headers, missing returns, unnecessary getter parameters and setters that use an undeclared value.

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

Repair recurring examination faults

OCR ERL listing:

procedure getScore(newScore)
    score = input("Score")
endprocedure

procedure setScore()
    score = newScore
endprocedure

There are several separate faults. The getter is written as a procedure, takes an unnecessary parameter, asks for input and overwrites state instead of returning it. The setter has no parameter, so newScore has no source. Repair the declarations and the direction in which data should move.

Written answer4 marks

Rewrite getScore and setScore in OCR ERL. The getter must take no unnecessary parameter and return the existing score. The setter must receive newScore and assign it.

Show both complete headers and bodies. Do not request input inside the getter.

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.