Object-Oriented Programming Techniques: Classes, Inheritance & Encapsulation (OCR A-Level CS 2.2.1)
OCR A-Level CS 2.2.1(f): object-oriented programming: classes and objects, attributes and methods, encapsulation, inheritance and polymorphism, with code examples.

Free OOP Techniques revision resources (OCR A-Level Computer Science, 2.2.1)
We’ve made exam-style practice for this exact topic, free to download: OOP Techniques question sheet, mark scheme and cheat sheet. Grab them, have a go, then read the full guide below.
If you do one thing to prepare for Paper 2, get fluent at this. Object-oriented programming (OOP) models a problem as a set of objects that hold their own data and behaviour, and spec point 2.2.1(f) asks you to actually use it. I'll be honest with you: this is one of the single biggest sources of marks on the whole paper. Examiners test it relentlessly, with "write the class", "write the constructor" and "write the get/set method" tasks, plus a regular 9 to 12 mark essay comparing OOP with procedural programming. The good news is that nearly all of it comes down to a small handful of patterns you can drill until they are automatic. That is exactly what we are going to do.

Classes and objects
A class is a blueprint (template) that defines what a kind of thing has and can do. An object is a specific instance created from that class. Making an object is called instantiation.
Attributes (fields/properties) are the data an object holds. A
Doghas anameand abreed.Methods are the subroutines that define what an object can do. A
Dogcanbark().
In OCR pseudocode, a class with a constructor (a special method, often new, that sets up a new object) looks like this:
You then instantiate objects from the class:
rex is an object of the Dog class; each object has its own copy of the attributes.
Encapsulation

Encapsulation means bundling the data and methods together inside the object, and keeping the data private so it can only be changed through the object's own methods. Attributes are marked private, and accessed through get and set methods (accessors and mutators):
Why bother? This is the most important part of encapsulation, and a favourite exam question. Keeping the data private protects it from being put into an invalid state by outside code, lets you add validation in the setter (for example, refusing a negative price), and means you can change the inside of the class without breaking the rest of the program. So if you are ever asked "explain how a private attribute improves the program", that is your answer ready to go.
Inheritance
Inheritance lets a new class (the child / subclass) take on the attributes and methods of an existing class (the parent / superclass), then add or change its own. It models an "is-a" relationship and avoids repeating code.
Puppy automatically has name, breed, getName() and bark() from Dog, plus its own play(). Define shared behaviour once in the parent; specialise in the children.
Polymorphism
Polymorphism ("many forms") lets different classes respond to the same method call in their own way. If Dog and Cat both have a makeSound() method, calling makeSound() on each gives the right result, "woof" or "meow", even though the call looks identical. A child class can override a parent's method to provide its own version. This lets you write general code (e.g. loop through a list of Animals calling makeSound()) that works for every subclass.
Worked example: writing a constructor and a getter
"Write the constructor for a Book class storing a title and price, and a method to return the price."
That is the exact pattern behind dozens of Paper 2 marks: private attributes, a constructor that sets them, and get/set methods to reach them safely. Learn this skeleton cold and you can adapt it to almost any "write the class" question they give you.
Common exam mistakes
These are the mix-ups I see most, and the good news is they are all quick to fix.
Class vs object. A class is the blueprint; an object is an instance made from it.
Dogis the class,rexis an object.Forgetting
private+ accessors. Encapsulation needs attributes private, reached via get/set methods.Inheritance direction. The child inherits from the parent (
Puppy inherits Dog), not the other way round.Polymorphism confusion. It's the same method name behaving differently per class (often via overriding).
One-sided OOP essays. "Compare OOP and procedural" wants the benefits of OOP and cases where procedural is simpler.
Quick recap
A class is a blueprint (attributes + methods); an object is an instance of it (created by instantiation).
Encapsulation: keep attributes private, access them through get/set methods, to protect data and allow validation.
Inheritance: a child class reuses a parent's attributes and methods (an "is-a" relationship), avoiding repetition.
Polymorphism: the same method call behaves differently for different classes (often via overriding).
OOP is examined with write-class/constructor/method tasks and an OOP-vs-procedural essay.
Here is the honest truth about OOP: it feels abstract until you have written it a few times, and then it suddenly clicks and never un-clicks. So do not just read the code in this post. Cover it up and rewrite the Dog class and the Book constructor from memory. Do that two or three times and you will walk into the exam genuinely fluent, not just familiar. Work hard now, enjoy later. I believe in you.
Frequently asked questions
What is the difference between a class and an object? A class is a blueprint or template that defines the attributes (data) and methods (behaviour) a kind of object will have. An object is a specific instance created from that class, with its own values for the attributes. For example, Dog is a class, while rex is an object of that class.
What is encapsulation? Encapsulation means bundling an object's data and methods together and keeping the data private, so it can only be read or changed through the object's own get and set methods. This protects the data from being put into an invalid state, allows validation in the setters, and lets the class's internals change without breaking other code.
What is inheritance? Inheritance lets a child class take on the attributes and methods of an existing parent class and then add or override its own. It models an "is-a" relationship and avoids repeating shared code, because common behaviour is defined once in the parent and reused by every child class.
What is polymorphism? Polymorphism means different classes can respond to the same method call in their own way. For example, calling makeSound() on a Dog and a Cat gives different results, because each class provides its own version of the method, often by overriding the parent's method.
What is a constructor? A constructor is a special method, often called new, that runs when an object is created and sets up its initial attribute values. For example, a Dog constructor might take a name and breed and store them in the new object's private attributes.
What are the advantages of object-oriented programming? OOP keeps related data and methods together, uses encapsulation to protect data, and uses inheritance and polymorphism to reuse and extend code, which makes large programs easier to maintain, reuse and develop in teams. For small or simple problems, however, a procedural approach can be quicker and simpler.


