Skip to main content
EnglandComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.814 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

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

Sources & how we know this