How do you define classes, create objects and apply inheritance and polymorphism in code, and when is OOP the right choice?
Object-oriented programming techniques in practice: defining classes with attributes and methods, constructors and instantiation, getters and setters for encapsulation, inheritance and method overriding for polymorphism, and the benefits of an object-oriented design.
An OCR H446 answer on object-oriented programming techniques in practice: defining classes with attributes and methods, constructors and instantiation, getters and setters for encapsulation, inheritance with method overriding for polymorphism, and the benefits of object-oriented design.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
OCR wants you to define classes in code: attributes and methods, constructors, instantiation, getters and setters for encapsulation, inheritance with method overriding for polymorphism, and the benefits of object-oriented design. Expect to write a class definition and to design a small class hierarchy.
The answer
Defining a class and creating objects
class Account
private balance
public procedure new(opening) // constructor
balance = opening
endprocedure
public function getBalance() // getter
return balance
endfunction
endclass
myAccount = new Account(100) // instantiation
Encapsulation with getters and setters
Inheritance and polymorphism in code
Examples in context
A game models Player, Enemy and Boss as subclasses of Character, overriding attack() so one game loop drives them all. A GUI library has Button and TextBox inherit from Control, overriding draw(). Banking systems model Account types with shared and specialised behaviour. Encapsulation is why you change an object only through its methods. OCR links this to the OOP principles in Component 01, to subroutines and scope, and to designing the data model for the Programming Project.
Try this
Q1. State what a constructor does. [1 mark]
- Cue. It is a method run when an object is instantiated, initialising the object's attributes.
Q2. Explain why attributes are made private with getters and setters. [2 marks]
- Cue. To enforce encapsulation: external code cannot set an invalid value directly, because access goes through methods that can validate, protecting the object's state.
Q3. State how polymorphism lets one loop process objects of different subclasses. [2 marks]
- Cue. Each subclass overrides the called method, so the version matching each object's actual class runs, giving correct behaviour without type-checking code.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20208 marksA program models library members and staff, who share some attributes but differ in others. Explain how a class hierarchy with inheritance and polymorphism would be designed, referring to encapsulation.Show worked answer →
Levels-of-response question; reward a designed hierarchy using all three OOP ideas.
Design a superclass Person holding shared attributes (name, ID) and methods, with attributes kept private (encapsulation) and accessed through getters and setters so they cannot be set to invalid values. Subclasses Member and Staff inherit from Person and add their own attributes (Member: booksOnLoan; Staff: role, salary) and methods, reusing the shared code.
Polymorphism: a method such as getDetails() is defined in Person and overridden in each subclass so it includes the subclass-specific information; a single loop over a list of Person objects can call getDetails() and the correct version runs for each. Encapsulation keeps each object's data safe behind methods. Top marks need a sensible superclass/subclass split, private attributes with accessors, and an overridden method demonstrating polymorphism.
OCR 20216 marksWrite a class definition in pseudocode (or a language you have studied) for a Rectangle with private width and height attributes, a constructor, a method to return the area, and a getter for the width.Show worked answer →
Award marks for the class structure, private attributes, constructor, area method and getter (up to 6).
class Rectangle
private width
private height
public procedure new(w, h)
width = w
height = h
endprocedure
public function getArea()
return width * height
endfunction
public function getWidth()
return width
endfunction
endclass
Reward declaring the attributes as private, a constructor that initialises them, an area function returning width times height, and a getter returning width. A common error is making attributes public, which breaks encapsulation.
Related dot points
- Thinking abstractly: the nature and need for abstraction, representational and procedural abstraction, and the use of models; thinking ahead and decomposition: breaking a problem into smaller sub-problems.
An OCR H446 answer on the computational thinking skills of abstraction and decomposition: the nature and need for abstraction, representational and procedural abstraction and the use of models, and decomposing a problem into smaller, more manageable sub-problems.
- Programming techniques: sequence, selection and iteration, recursion, the use of subroutines (procedures and functions) with parameters passed by value and by reference, local and global variable scope, and the features of an integrated development environment (IDE).
An OCR H446 answer on programming techniques: sequence, selection and iteration, recursion, subroutines (procedures and functions) with parameters passed by value or by reference, local and global variable scope, and the features of an integrated development environment.
- Programming paradigms (procedural, low-level / assembly and object-oriented), the need for and characteristics of different levels of programming language, and the core principles of object-oriented programming: classes, objects, methods, attributes, encapsulation, inheritance and polymorphism.
An OCR H446 answer on programming paradigms and language levels: procedural, low-level and object-oriented programming, the characteristics of high-level versus low-level languages, and the OOP principles of classes, objects, methods, attributes, encapsulation, inheritance and polymorphism.
- Thinking logically: identifying the decision points and conditions that affect the flow of a solution; computational methods including problem recognition, divide and conquer, backtracking, heuristics, performance modelling and visualisation.
An OCR H446 answer on thinking logically and computational methods: identifying decision points and conditions in a solution, and the methods of problem recognition, divide and conquer, backtracking, heuristics, performance modelling and visualisation that make problems solvable.
- Data structures: arrays, records, tuples and lists, the stack and queue abstract data types and their operations, linked lists, trees and graphs, and hash tables, including how each is used and its advantages.
An OCR H446 answer on data structures: arrays, records, tuples and lists, the stack and queue abstract data types with their operations, linked lists, trees, graphs and hash tables, including how each is used and its advantages and disadvantages.