Encapsulation (OOP) (Cambridge (CIE) A Level Computer Science): Revision Note
Exam code: 9618
Encapsulation (OOP)
What is encapsulation?
In A Level Computer Science, encapsulation refers to the practice of grouping data (attributes) and methods (functions) within a class
Using encapsulation ensures that data remains secure and is not accidentally modified or misused by controlling access to them using access modifiers (e.g., public, private)
It also helps to organize code by keeping related data and methods together within an object
Encapsulation promotes code reusability, which means the same object or class can be used in different parts of a program without rewriting the code
Encapsulation uses a concept called “Abstraction” which reduces complexity by hiding the implementation details of the object, making it easier to understand and work with
Programmers can use methods and classes from other parts of the program without having to understand how that it has been constructed internally
Encapsulation in classes
Private variables are only accessible within the class itself, and external code cannot access them directly.
Encapsulation hides how things work inside a class from the outside. External code can interact with the class using public methods without needing to understand its internal details

Encapsulation in classes
Encapsulation in methods

Encapsulation in methods
Examiner Tips and Tricks
When determining whether a method or attribute is public or private, if neither keyword appears, then assume it is public
Programming encapsulation (OOP)
What is a get method?
The "get" method is a common naming convention for a type of method that is used to retrieve the value of an object's private attributes (instance variables)
These methods are also known as "getter" methods or "accessor" methods
The main purpose of a get method is to provide controlled access to the internal state (data) of an object without allowing direct modification
Attributes are often declared as private to achieve encapsulation, so cannot be accessed directly from outside the class
External code can use the public get methods to read the values of these private attributes (also known as instance variables)
Pseudocode

Pseudocode for a get method
Java
//creating a class called NumberUpdater
public class NumberUpdater {
//setting the private attribute to the value of 10
private int number = 10;
//creating a get method that will return the value store in the number attribute
public int getNumber() {
return number;
}
}
Python
#creating a class called NumberUpdater
class NumberUpdater:
#setting the private attribute to the value of 10
def __init__(self):
self.__number = 10
#creating a get method that will return the value stored in the number attribute
@property
def number(self):
return self.__number
What is a set method?
The "set" method is a common naming convention for a type of method that is used to set the value of object's private instance variables
Setter methods, also known as “setter” methods or “mutator” methods
Generally additional code is written within the method to allow controlled access by enforcing certain conditions or validations before updating the attribute
Pseudocode

Pseudocode for a "setter" method
Java
//creating a class called NumberUpdater
public class NumberUpdater
//setting attribute value to 10
private int oldnumber = 10;
//creating a set method
public void setNumber(int newnumber) {
//if the new number is less than zero
if (newnumber < 0) {
//oldnumber is kept the same
oldnumber = oldnumber;
} else {
//oldnumber is updated with the new number value
oldnumber = newnumber;
}
}
}
Python
//creating a class called NumberUpdater
class NumberUpdater:
//setting attribute value to 10
def __init__(self):
self.__oldnumber = 10
//creating a set method
def setNumber(self, newnumber):
//if the new number is less than zero
if newnumber < 0:
//oldnumber is kept the same
self.__oldnumber = self.__oldnumber
else:
//oldnumber is updated with the new number value
self.__oldnumber = newnumber
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 for the get methods GetCurrentSpeed()
,
GetIncreaseAmount()
, GetMaxSpeed()
and GetHorizontalPosition()
[3]
Answer
1 get function header (and end where appropriate) with no parameter … [1 mark]
…returning attribute (without overwriting) [1 mark]
3 further correct get methods [1 mark]
Example program code:
VB.NET
Function GetCurrentSpeed()
Return CurrentSpeed
End Function
Function GetIncreaseAmount()
Return IncreaseAmount
End Function
Function GetHorizontalPosition()
Return HorizontalPosition
End Function
Function GetMaxSpeed()
Return MaxSpeed
End Function
Java
public Integer GetCurrentSpeed(){
return CurrentSpeed;
}
public Integer GetIncreaseAmount(){
return IncreaseAmount;
}
public Integer GetHorizontalPosition(){
return HorizontalPosition;
}
public Integer GetMaxSpeed(){
return MaxSpeed;
}
Python
def GetCurrentSpeed(self):
return self.__CurrentSpeed
def GetIncreaseAmount(self):
return self.__IncreaseAmount
def GetHorizontalPosition(self):
return self.__HorizontalPosition
def GetMaxSpeed(self):
return self.__MaxSpeed
You've read 0 of your 5 free revision notes this week
Unlock more, it's free!
Did this page help you?