Classes (OOP) (Cambridge (CIE) A Level Computer Science): Revision Note
Exam code: 9618
Classes (Object-Oriented Programming)
What is a class?
A class is a blueprint used to create objects in Object-Oriented Programming (OOP)
It defines a structure for attributes (data) and methods (behaviour)
Key terms
Term | Definition |
---|---|
Object | An instance of a class with its own state and behaviour |
Attribute | A variable defined in a class (also called a class variable) |
Instance Variable | Attribute stored inside an individual object |
Method | A function defined within a class; describes object behaviour |
Instantiation | The process of creating an object from a class |
Identifier | The name used to refer to an object |
Example
A
Student
class could contain:Attributes:
name
,dateOfBirth
,gender
Each
Student
object (e.g.John
,06/10/2015
,Male
) has its own values
Prebuilt vs custom classes
Prebuilt classes | Custom classes |
---|---|
Provided by the language | Defined by the programmer |
E.g. | E.g. |
Below is a visual representation of both a class and objects that have been instantiated
In the image, the identifiers are P1 and P2

Example of a class and objects
Programming classes (OOP)
How do you program a class?
In this example we will:
Define a class for a car sales program where the following is required:
Name of Manufacturer
Model of Car
Price
Mileage
Whether the car is preowned
Instantiate two objects for the car sales program ensuring that they have appropriate identifiers
List two methods that would be useful for a car object
Pseudocode

Pseudocode for creating classes, a constructor, and functions
Java
//creating a class called cars
public class Cars {
//creating class attributes for a car
private String Manufacturer;
private String Model;
private int Price;
private int Mileage;
private boolean PreOwned;
//Constructor - This is used to create the objects. More on this in the object chapter)
public Cars(String manufacturer, String model, int price, int mileage, boolean preOwned) {
this.Manufacturer = manufacturer;
this.Model = model;
this.Price = price;
this.Mileage = mileage;
this.PreOwned = preOwned;
}
//method to make the car go faster.
public void Accelerate(){
//code to be executed which will make the car go faster
}
//Method to honk horn
public void honkHorn(){
// code to be executed which will honk the car horn
}
}
Python
#creating a class called cars
class Cars:
# constructor - This is used to create the objects. More on this in the object chapter)
def __init__(self, manufacturer, model, price, mileage, preOwned):
self.Manufacturer = manufacturer
self.Model = model
self.Price = price
self.Mileage = mileage
self.PreOwned = preOwned
#method to make the car go faster.
def Accelerate(self):
# code to be executed which will make the car go faster
# method to honk horn
def honkHorn(self):
# code to be executed which will honk the car horn
Worked Example
A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.
The class Vehicle
stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount
is added to the current speed each time the vehicle increases its speed.

Write program code to declare the class Vehicle
. All attributes must be private.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language’s appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments. [5]
Answer
Class header (and close where appropriate) [1 mark]
5 (private) attribute declarations including data types [1 mark]
Constructor header (and close where appropriate) taking 3 parameters (min) [1 mark]
Assigning
ID, MaxSpeed
andIncreaseAmount
to parameters [1 mark]Assigning
CurrentSpeed
andHorizontalPosition
to 0 [1 mark]
Example program code:
VB.NET
Class Vehicle
Private ID As String
Private MaxSpeed As Integer
Private CurrentSpeed As Integer
Private IncreaseAmount As Integer
Private HorizontalPosition As Integer
Sub New(IDP, MaxSpeedP, IncreaseAmountP)
ID = IDP
MaxSpeed = MaxSpeedP
CurrentSpeed = 0
IncreaseAmount = IncreaseAmountP
HorizontalPosition = 0
End Sub
End Class
Java
class Vehicle{
private String ID;
private Integer MaxSpeed;
private Integer CurrentSpeed;
private Integer IncreaseAmount;
private Integer HorizontalPosition;
public Vehicle(String IDP, Integer MaxSpeedP, Integer IncreaseAmountP {
ID = IDP;
MaxSpeed = MaxSpeedP;
IncreaseAmount = IncreaseAmountP;
CurrentSpeed = 0;
HorizontalPosition = 0;
}}
Python
class Vehicle:
#self.__ID string
#self.__MaxSpeed integer
#self.__CurrentSpeed integer
#self.__IncreaseAmount integer
#self.__HorizontalPosition
def __init__(self, IDP, MaxSpeedP, IncreaseAmountP):
self.__ID = IDP
self.__MaxSpeed = MaxSpeedP
self.__IncreaseAmount = IncreaseAmountP
self.__CurrentSpeed = 0
self.__HorizontalPosition = 0
You've read 0 of your 5 free revision notes this week
Unlock more, it's free!
Did this page help you?