Encapsulation: private attributes, getters and setters – OCR A Level Computer Science lesson (H446)
Students learn to write a class with private attributes and public getters and setters, and explain how encapsulation protects data.
Lesson 5 of 13 in unit 12.6 Object-Oriented Programming, taught in Year 12, Spring 1. It answers one big question: Why should other code not change an object's data directly?
By the end, students can write a class with private attributes and public getters and setters, and explain how encapsulation protects data.
- The Build (the main task)
- Write Fernhollow's Ticket class with a private price attribute, a public getter, and a public setter that rejects a price below 0 or above 100, then explain how encapsulation protects the park's ticket data (7 marks).
- Support version
- The class skeleton with private and public already written; students fill in the getter's return and the setter's validation from a choice of three conditions.
- Challenge
- Explain why making the price attribute public would let a bug in the booking website charge £0 for every ticket.
- Real-world link
- A fault in Fernhollow's booking website must not be able to set a ticket price to −£5.
- Key words
- Encapsulation, Getter and Restrict, each with a definition, an icon and a common mix-up on the key vocabulary slide.
- Misconceptions it tackles
- “Getters and setters should be private”; “Encapsulation means data can never be changed”.
- OCR H446 specification
- 1.2.4 (e); 2.2.1 (f) (programming techniques: using object-oriented techniques when writing programs).
Every lesson follows the same Code & Commerce structure: a Bell Task recall starter, key vocabulary with definitions and icons, short teaching chunks with a check before pupils move on, The Build (one main task), challenge questions that get harder, and an exit question. Answers reveal one at a time on the slides, and everything is editable PowerPoint and Word.
Students need computers for the main task.
Also sold in the Object-Oriented Programming unit bundle, which includes the medium term plan, and in the full-year and complete-course bundles.
An independent resource written for OCR A Level Computer Science (H446). It is not endorsed by OCR or Cambridge University Press & Assessment. Programming project (NEA) lessons teach general skills on a made-up example project and give supervised time, within JCQ's rules on the help a centre may give.
What's included
- Slide deck (PowerPoint)
- Lesson plan (Word)
- Worksheet (Word)
- Support worksheet with scaffolds (Word)
- Single-teacher licence: use and adapt it with your own classes
More for A Level Computer Science
See allStudents learn to describe the characteristics of procedural and object-oriented languages and explain why different paradigms exist.
Free sample lessonStudents learn to explain the difference between a class and an object, and create several objects from a given class.
Students learn to write a constructor that sets attributes from parameters, and instantiate objects correctly in Python and OCR pseudocode.
Written for the specifications named. Not endorsed by or affiliated with any exam board.